Module: Awestruct::IBeams::AsciidocSections
- Defined in:
- lib/awestruct/ibeams/asciidoc_sections.rb
Instance Method Summary collapse
-
#find_subsections_from(section, in_document) ⇒ Array
This method will recurse through all the subsections of the given
section
to discover the full tree of subsections and compute a nested array containing them. -
#section_anchor(title, in_document) ⇒ String
Compute an appropriate section anchor for the given title within the given
Asciidoctor::Document
. -
#sections_from(directory) ⇒ Array
sections_from() will take the given.
Instance Method Details
#find_subsections_from(section, in_document) ⇒ Array
This method will recurse through all the subsections of the given section
to discover the full tree of subsections and compute a nested array containing them.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/awestruct/ibeams/asciidoc_sections.rb', line 65 def find_subsections_from(section, in_document) found = [] section.sections.each do |subsection| title = subsection.title found << [ title, { :asciidoc => in_document, :path => in_document.file, }, section_anchor(title, in_document), find_subsections_from(subsection, in_document), ] end return found end |
#section_anchor(title, in_document) ⇒ String
Compute an appropriate section anchor for the given title within the given Asciidoctor::Document
. This is effectively the same logic that Asciidoctor itself inside the Asciidoctor::Section#generate_id method but extracted to be callable in an idempotent fashion
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/awestruct/ibeams/asciidoc_sections.rb', line 95 def section_anchor(title, in_document) prefix = in_document.attributes['idprefix'] || '' separator = in_document.attributes['idseparator'] || '-' title = title.downcase.gsub(Asciidoctor::InvalidSectionIdCharsRx, '') return [ prefix, title.tr_s(" ._-", separator).chomp(separator).delete_prefix(separator), ].join('') end |
#sections_from(directory) ⇒ Array
sections_from() will take the given
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/awestruct/ibeams/asciidoc_sections.rb', line 16 def sections_from(directory) if directory.nil? raise Errors::InvalidPath.new("The provided `directory` was nil") end unless File.directory?(directory) raise Errors::InvalidPath.new("The provided `#{directory}` is not a valid directory") end # Generate a +Hash+ of all the pages in the site keyed by their path # for easy discovery pages_by_path = site.pages.map { |p| [p.source_path, p] }.to_h sections = [] Naturally.sort(Dir.glob(File.join(directory, '*.{ad,adoc}'))).each do |adoc| # Since all our documents are going to have front-matter so they render # properly, we need to look first at the Awestruct::Page and then # re-render to grab the sections from the asciidoc page = pages_by_path[adoc] url = page.url document = Asciidoctor.load(page.raw_content) document.sections.each do |section| sections << [ section.title, { :asciidoc => document, :path => adoc, :page => page, }, url, find_subsections_from(section, document) ] end end return sections end |