diff --git a/examples/config.yml b/examples/config.yml index 515549c..ace7f8d 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -45,6 +45,9 @@ css_classes: &css_classes word_count: format: word_count +chapters: + format: chapters + summary_plain: format: summary_plain diff --git a/examples/full.chapters.json b/examples/full.chapters.json new file mode 100644 index 0000000..634cbed --- /dev/null +++ b/examples/full.chapters.json @@ -0,0 +1,16 @@ +[ + { + "slug": "chapter-1", + "title": "Chapter 1", + "chapters": [ + + ] + }, + { + "slug": "chapter-2", + "title": "Chapter 2", + "chapters": [ + + ] + } +] diff --git a/examples/full.repub b/examples/full.repub index 57ac233..81bf7d2 100644 --- a/examples/full.repub +++ b/examples/full.repub @@ -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 diff --git a/examples/hello.chapters.json b/examples/hello.chapters.json new file mode 100644 index 0000000..41b42e6 --- /dev/null +++ b/examples/hello.chapters.json @@ -0,0 +1,3 @@ +[ + +] diff --git a/exe/repubmark b/exe/repubmark index fa15677..1d52bab 100755 --- a/exe/repubmark +++ b/exe/repubmark @@ -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? diff --git a/lib/repubmark.rb b/lib/repubmark.rb index 5ea22d5..8f79d5b 100644 --- a/lib/repubmark.rb +++ b/lib/repubmark.rb @@ -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/ diff --git a/lib/repubmark/elems/article.rb b/lib/repubmark/elems/article.rb index 8041dce..bec8aa3 100644 --- a/lib/repubmark/elems/article.rb +++ b/lib/repubmark/elems/article.rb @@ -45,6 +45,8 @@ module Repubmark ].compact.join("\n\n\n").freeze end + def chapters = @chapter&.chapters[:chapters] || [].freeze + ################### # Builder methods # ################### diff --git a/lib/repubmark/elems/chapter.rb b/lib/repubmark/elems/chapter.rb index ac9e6a6..308a5eb 100644 --- a/lib/repubmark/elems/chapter.rb +++ b/lib/repubmark/elems/chapter.rb @@ -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 diff --git a/test.rb b/test.rb index 14eaf43..d93278a 100755 --- a/test.rb +++ b/test.rb @@ -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|