Module: Howdoc::Registry
- Defined in:
- lib/howdoc/registry.rb
Overview
The index, assembled at the end of a run from the guides themselves.
Tests run in forked workers, so nothing held in memory survives a suite and the index has to be built from what is on disk. What is on disk is the guides -- and their markup is not a foreign format to be parsed defensively, because this gem wrote it. One element carries everything the index needs.
That is the entire contract with an overriding template: keep the document heading in
Defined Under Namespace
Classes: Record
Constant Summary collapse
- INDEX_FILENAME =
'index.html'- TITLE =
%r{<title[^>]*>(.*?)</title>}mi- HEADING =
A heading opens with an identifier when it reads like "1.3. Some title". The identifier has to carry a digit, so an ordinary sentence beginning "Mr. Smith" is not mistaken for one.
/\A([\w.]*\d[\w.]*)\.\s+(.+)\z/m
Class Method Summary collapse
- .by_locale(root = Howdoc.config.root) ⇒ Object
-
.clean(root = Howdoc.config.root) ⇒ Object
Removes generated guides while leaving hand-maintained pages in place, so a run never inherits a guide whose test has since been deleted.
-
.generated_page?(basename) ⇒ Boolean
The index is not a guide, and neither is a landing page somebody maintains by hand.
- .read(path) ⇒ Object
- .records(root = Howdoc.config.root) ⇒ Object
- .sort(records) ⇒ Object
- .split_heading(heading) ⇒ Object
- .title_of(markup) ⇒ Object
-
.write_indexes(root = Howdoc.config.root) ⇒ Object
Builds the index for every locale that produced at least one guide, and then writes the menu into the guides themselves: they were written one at a time, before there was a menu to draw.
Class Method Details
.by_locale(root = Howdoc.config.root) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/howdoc/registry.rb', line 76 def by_locale(root = Howdoc.config.root) found = records(root).group_by(&:locale) return found if Howdoc.config.locales.nil? Howdoc.config.locales.to_h { |locale| [locale.to_s, found.fetch(locale.to_s, [])] } end |
.clean(root = Howdoc.config.root) ⇒ Object
Removes generated guides while leaving hand-maintained pages in place, so a run never inherits a guide whose test has since been deleted.
Only what this gem writes is removed. An output directory usually holds other things -- a landing page, a stylesheet an application maintains by hand -- and a cleaner that swept the whole directory would eat them.
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/howdoc/registry.rb', line 112 def clean(root = Howdoc.config.root) Dir.glob(File.join(root, '*')).each do |dir| next unless File.directory?(dir) FileUtils.rm_rf(File.join(dir, 'images')) Dir.glob(File.join(dir, '*.html')).each do |entry| next if Howdoc.config.preserved_files.include?(File.basename(entry)) FileUtils.rm_f(entry) end end end |
.generated_page?(basename) ⇒ Boolean
The index is not a guide, and neither is a landing page somebody maintains by hand.
60 61 62 |
# File 'lib/howdoc/registry.rb', line 60 def generated_page?(basename) basename == INDEX_FILENAME || Howdoc.config.preserved_files.include?(basename) end |
.read(path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/howdoc/registry.rb', line 42 def read(path) basename = File.basename(path) return nil if generated_page?(basename) heading = title_of(File.read(path)) return nil if heading.nil? || heading.empty? id, title = split_heading(heading) Record.new( locale: File.basename(File.dirname(path)), permalink: File.basename(basename, '.html'), heading:, id:, title: ) end |
.records(root = Howdoc.config.root) ⇒ Object
38 39 40 |
# File 'lib/howdoc/registry.rb', line 38 def records(root = Howdoc.config.root) Dir.glob(File.join(root, '*', '*.html')).filter_map { |path| read(path) } end |
.sort(records) ⇒ Object
102 103 104 |
# File 'lib/howdoc/registry.rb', line 102 def sort(records) records.sort_by { |record| Howdoc.config.sort_key.call(record).to_s } end |
.split_heading(heading) ⇒ Object
71 72 73 74 |
# File 'lib/howdoc/registry.rb', line 71 def split_heading(heading) match = heading.match(HEADING) match ? [match[1], match[2]] : [nil, heading] end |
.title_of(markup) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/howdoc/registry.rb', line 64 def title_of(markup) match = markup.match(TITLE) return nil if match.nil? CGI.unescapeHTML(match[1]).strip end |
.write_indexes(root = Howdoc.config.root) ⇒ Object
Builds the index for every locale that produced at least one guide, and then writes the menu into the guides themselves: they were written one at a time, before there was a menu to draw.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/howdoc/registry.rb', line 86 def write_indexes(root = Howdoc.config.root) Templates.install_assets(File.join(root, 'assets')) if Howdoc.config.install_assets catalogue = by_locale(root).transform_values { |records| sort(records) } written = catalogue.map do |locale, records| Writers::Index.call( locale:, records:, root:, languages: Navigation.languages(current_locale: locale, by_locale: catalogue) ) end Navigation.install(catalogue, root:) written end |