Compare commits

...

2 commits

Author SHA1 Message Date
63dbec9a76
Add format "chapters" 2024-09-28 03:10:33 +04:00
633623d299
Specify Ruby version and gemset 2024-09-28 03:08:56 +04:00
10 changed files with 57 additions and 11 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/Gemfile.lock /Gemfile.lock
/.ruby-version
/.ruby-gemset

View file

@ -45,6 +45,9 @@ css_classes: &css_classes
word_count: word_count:
format: word_count format: word_count
chapters:
format: chapters
summary_plain: summary_plain:
format: 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| chapter.paragraph do |paragraph|
paragraph.text 'Chapter 1 paragraph text' paragraph.text 'Chapter 1 paragraph text'
end end
@ -41,7 +41,7 @@ end
article.chapter 'Chapter 2' do |chapter| article.chapter 'chapter-2', 'Chapter 2' do |chapter|
chapter.paragraph do |paragraph| chapter.paragraph do |paragraph|
paragraph.text 'Chapter 2 paragraph text' paragraph.text 'Chapter 2 paragraph text'
end end

View file

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

View file

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

View file

@ -64,7 +64,7 @@ require_relative 'repubmark/elems/power'
require_relative 'repubmark/elems/footnote' require_relative 'repubmark/elems/footnote'
module Repubmark 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/ SLUG_RE = /\A\w+(-\w+)*\z/

View file

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

View file

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

View file

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