Add format "chapters"

This commit is contained in:
Alex Kotov 2024-09-28 02:49:30 +04:00
parent 633623d299
commit 63dbec9a76
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
9 changed files with 55 additions and 11 deletions

View file

@ -45,6 +45,9 @@ css_classes: &css_classes
word_count:
format: word_count
chapters:
format: chapters
summary_plain:
format: summary_plain

View file

@ -0,0 +1,16 @@
[
{
"slug": "chapter-1",
"title": "Chapter 1",
"chapters": [
]
},
{
"slug": "chapter-2",
"title": "Chapter 2",
"chapters": [
]
}
]

View file

@ -33,7 +33,7 @@ end
article.chapter 'Chapter 1' do |chapter|
article.chapter 'chapter-1', 'Chapter 1' do |chapter|
chapter.paragraph do |paragraph|
paragraph.text 'Chapter 1 paragraph text'
end
@ -41,7 +41,7 @@ end
article.chapter 'Chapter 2' do |chapter|
article.chapter 'chapter-2', 'Chapter 2' do |chapter|
chapter.paragraph do |paragraph|
paragraph.text 'Chapter 2 paragraph text'
end

View file

@ -0,0 +1,3 @@
[
]

View file

@ -36,10 +36,11 @@ end
output =
case config.format
when :word_count then $article.word_count
when :summary_plain then wrap $article.to_summary_plain
when :html then $article.to_html
when :chapters then JSON.pretty_generate $article.chapters
when :gemtext then $article.to_gemtext
when :html then $article.to_html
when :summary_plain then wrap $article.to_summary_plain
when :word_count then $article.word_count
end.to_s.strip
puts output unless output.empty?

View file

@ -64,7 +64,7 @@ require_relative 'repubmark/elems/power'
require_relative 'repubmark/elems/footnote'
module Repubmark
FORMATS = %i[gemtext html summary_plain word_count].freeze
FORMATS = %i[chapters gemtext html summary_plain word_count].freeze
SLUG_RE = /\A\w+(-\w+)*\z/

View file

@ -45,6 +45,8 @@ module Repubmark
].compact.join("\n\n\n").freeze
end
def chapters = @chapter&.chapters[:chapters] || [].freeze
###################
# Builder methods #
###################

View file

@ -5,9 +5,10 @@ module Repubmark
class Chapter < Base
parents :Article, :Chapter
def initialize(parent, level = 1, title = nil)
def initialize(parent, level = 1, slug = nil, title = nil)
super parent
self.level = level
self.slug = slug
self.title = title
verify!
@ -51,12 +52,20 @@ module Repubmark
].join.freeze
end
def chapters
{
slug: @slug,
title: @title,
chapters: @chapters.map(&:chapters).freeze,
}.freeze
end
###################
# Builder methods #
###################
def chapter(title)
chapter = Chapter.new self, @level + 1, title
def chapter(slug, title)
chapter = Chapter.new self, @level + 1, slug, title
@chapters << chapter
yield chapter
nil
@ -85,6 +94,10 @@ module Repubmark
@level = level
end
def slug=(slug)
@slug = Repubmark.validate_slug! slug if slug
end
def title=(title)
return @title = nil if title.nil?
@ -95,8 +108,13 @@ module Repubmark
end
def verify!
raise 'Non-empty title for level 1' if @level == 1 && @title
raise 'Empty title for level >= 2' if @level != 1 && @title.nil?
if @level == 1
raise 'Non-empty slug for level 1' if @slug
raise 'Non-empty title for level 1' if @title
else
raise 'Empty slug for level >= 2' if @slug.nil?
raise 'Empty title for level >= 2' if @title.nil?
end
end
def build_title_html

View file

@ -12,6 +12,7 @@ PROFILES = [
%w[summary_plain summary.txt],
%w[http html],
%w[gemini gmi],
%w[chapters chapters.json],
].map(&:freeze).freeze
Dir[EXAMPLES_GLOB].each do |example_filename|