diff options
| author | nsfisis <nsfisis@gmail.com> | 2022-11-19 14:23:32 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2022-11-19 14:25:59 +0900 |
| commit | 6209453817da9922f28bac1bb1522c6d380630ab (patch) | |
| tree | 19e0699e751af387d549d6720ca215c8065b3c0c /lib/extensions | |
| parent | 0cafa073914b5e0b162b735a7f8445fb2aa8a604 (diff) | |
| download | blog.nsfisis.dev-6209453817da9922f28bac1bb1522c6d380630ab.tar.gz blog.nsfisis.dev-6209453817da9922f28bac1bb1522c6d380630ab.tar.zst blog.nsfisis.dev-6209453817da9922f28bac1bb1522c6d380630ab.zip | |
Hugo to Asciidoctor
Diffstat (limited to 'lib/extensions')
| -rw-r--r-- | lib/extensions/document_title_processor.rb | 15 | ||||
| -rw-r--r-- | lib/extensions/lang_attribute_processor.rb | 9 | ||||
| -rw-r--r-- | lib/extensions/revision_history_processor.rb | 27 | ||||
| -rw-r--r-- | lib/extensions/section_id_validator.rb | 29 | ||||
| -rw-r--r-- | lib/extensions/source_id_processor.rb | 12 | ||||
| -rw-r--r-- | lib/extensions/source_id_validator.rb | 32 | ||||
| -rw-r--r-- | lib/extensions/tags_processor.rb | 20 |
7 files changed, 144 insertions, 0 deletions
diff --git a/lib/extensions/document_title_processor.rb b/lib/extensions/document_title_processor.rb new file mode 100644 index 0000000..fd25844 --- /dev/null +++ b/lib/extensions/document_title_processor.rb @@ -0,0 +1,15 @@ +module Nuldoc + module Extensions + class DocumentTitleProcessor < Asciidoctor::Extensions::TreeProcessor + def process(doc) + doc.title = substitute_document_title(doc.title) + end + + private + + def substitute_document_title(title) + title.sub(/\A\[(.+?)\] /, '【\1】') + end + end + end +end diff --git a/lib/extensions/lang_attribute_processor.rb b/lib/extensions/lang_attribute_processor.rb new file mode 100644 index 0000000..65511bc --- /dev/null +++ b/lib/extensions/lang_attribute_processor.rb @@ -0,0 +1,9 @@ +module Nuldoc + module Extensions + class LangAttributeProcessor < Asciidoctor::Extensions::TreeProcessor + def process(doc) + doc.attributes['lang'] ||= 'ja-JP' + end + end + end +end diff --git a/lib/extensions/revision_history_processor.rb b/lib/extensions/revision_history_processor.rb new file mode 100644 index 0000000..f416de0 --- /dev/null +++ b/lib/extensions/revision_history_processor.rb @@ -0,0 +1,27 @@ +module Nuldoc + module Extensions + class RevisionHistoryProcessor < Asciidoctor::Extensions::TreeProcessor + def process(doc) + revisions = [] + i = 1 + loop do + break unless (rev = doc.attributes["revision-#{i}"]) + revisions << parse_revision(rev) + i += 1 + end + doc.attributes['revision-history'] = revisions + end + + private + + def parse_revision(rev) + m = rev.match(/\A(\d\d\d\d-\d\d-\d\d) (.*)\z/) + raise unless m + Revision.new( + date: Date.parse(m[1], '%Y-%m-%d'), + remark: m[2], + ) + end + end + end +end diff --git a/lib/extensions/section_id_validator.rb b/lib/extensions/section_id_validator.rb new file mode 100644 index 0000000..2ad496c --- /dev/null +++ b/lib/extensions/section_id_validator.rb @@ -0,0 +1,29 @@ +module Nuldoc + module Extensions + class SectionIdValidator < Asciidoctor::Extensions::TreeProcessor + def process(doc) + errors = [] + (doc.find_by(context: :section) {_1.level > 0}).each do |section| + errors << validate_section(section) + end + error_message = errors.compact.join("\n") + unless error_message.empty? + raise "SectionIdValidator (#{doc.attributes['source-file-path']}):\n#{error_message}" + end + end + + private + + def validate_section(section) + id = section.id + unless id + return "Section '#{section.title}': each section MUST have an id." + end + unless id.match?(/\A[-0-9a-z]+\z/) + return "Section '#{section.title}' (##{id}): section id MUST consist of either hyphen, digits or lowercases." + end + nil + end + end + end +end diff --git a/lib/extensions/source_id_processor.rb b/lib/extensions/source_id_processor.rb new file mode 100644 index 0000000..13813e0 --- /dev/null +++ b/lib/extensions/source_id_processor.rb @@ -0,0 +1,12 @@ +module Nuldoc + module Extensions + class SourceIdProcessor < Asciidoctor::Extensions::TreeProcessor + def process(doc) + errors = [] + (doc.find_by(context: :listing) {_1.style == 'source'}).each do |source| + source.id = "source.#{source.id}" + end + end + end + end +end diff --git a/lib/extensions/source_id_validator.rb b/lib/extensions/source_id_validator.rb new file mode 100644 index 0000000..6e04deb --- /dev/null +++ b/lib/extensions/source_id_validator.rb @@ -0,0 +1,32 @@ +module Nuldoc + module Extensions + class SourceIdValidator < Asciidoctor::Extensions::TreeProcessor + def process(doc) + errors = [] + (doc.find_by(context: :listing) {_1.style == 'source'}).each do |source| + errors << validate_section(source) + end + error_message = errors.compact.join("\n") + unless error_message.empty? + raise "SourceIdValidator (#{doc.attributes['source-file-path']}):\n#{error_message}" + end + end + + private + + def validate_section(source) + id = source.id + unless id + return "Each source MUST have an id." + end + if id.start_with?('source.') + return "Source id (##{id}) MUST NOT start with 'source.', which is appended by `nul`." + end + unless id.match?(/\A[-0-9a-z]+\z/) + return "Source id (##{id}) MUST consist of either hypen, digits or lowercases." + end + nil + end + end + end +end diff --git a/lib/extensions/tags_processor.rb b/lib/extensions/tags_processor.rb new file mode 100644 index 0000000..efbd2a8 --- /dev/null +++ b/lib/extensions/tags_processor.rb @@ -0,0 +1,20 @@ +module Nuldoc + module Extensions + class TagsProcessor < Asciidoctor::Extensions::TreeProcessor + def process(doc) + doc.attributes['tags'] = convert_tags(doc.attributes['tags']) + end + + private + + def convert_tags(tags) + return [] unless tags + + tags + .split(',') + .map(&:strip) + .map { Tag.from_slug(_1) } + end + end + end +end |
