Class: Giblish::HeadingIndexer

Inherits:
Asciidoctor::Extensions::TreeProcessor
  • Object
show all
Defined in:
lib/giblish/search/headingindexer.rb

Overview

Implements both an Asciidoctor TreeProcessor hook and a giblish post-build hook.

The TreeProcessor hook indexes all headings found in all documents in the tree and copies a ‘washed’ version of the source lines to a search asset folder in the destination tree.

The post build hook copies the completed heading index to the search assets folder.

Format of the heading index database: {

fileinfos : [{
  filepath : filepath_1,
  title : Title,
  sections : [{
    id : section_id_1,
    title : section_title_1,
    line_no : line_no
  },
  {
    id : section_id_1,
    title : section_title_1,
    line_no : line_no
  },
  ...
  ]
},
{
  filepath : filepath_2,
  ...
}]

}

Constant Summary collapse

HEADING_REGEX =
/^=+\s+(.*)$/
ANCHOR_REGEX =
/^\[\[(\w+)\]\]\s*$/
HEADING_DB_BASENAME =
"heading_db.json"
SEARCH_ASSET_DIRNAME =
"gibsearch_assets"

Instance Method Summary collapse

Constructor Details

#initialize(src_topdir) ⇒ HeadingIndexer

src_topdir

a Pathname to the top dir of the src files



52
53
54
55
56
57
# File 'lib/giblish/search/headingindexer.rb', line 52

def initialize(src_topdir)
  super({})

  @src_topdir = src_topdir
  @heading_index = {fileinfos: []}
end

Instance Method Details

#on_postbuild(src_topdir, dst_tree, converter) ⇒ Object

called by the TreeConverter during the post_build phase



85
86
87
88
89
90
# File 'lib/giblish/search/headingindexer.rb', line 85

def on_postbuild(src_topdir, dst_tree, converter)
  search_topdir = dst_tree.pathname / SEARCH_ASSET_DIRNAME

  # store the JSON file
  serialize_section_index(search_topdir, search_topdir)
end

#process(document) ⇒ Object

called by Asciidoctor during the conversion of the document.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/giblish/search/headingindexer.rb', line 60

def process(document)
  attrs = document.attributes
  src_node = attrs["giblish-info"][:src_node]

  # only index source files that reside on the 'physical' file system
  return if src_node.nil? || !src_node.pathname.exist?

  # make sure we use the correct id elements when indexing
  # sections
  opts = {
    id_prefix: (attrs.key?("idprefix") ? attrs["idprefix"] : "_"),
    id_separator: (attrs.key?("id_separator") ? attrs["id_separator"] : "_")
  }

  # index sections and wash source lines
  # Copy the washed document to the search asset folder
  dst_top = attrs["giblish-info"][:dst_top]
  write_washed_doc(
    parse_document(document, src_node, opts),
    dst_top.pathname / SEARCH_ASSET_DIRNAME / rel_src_path(src_node)
  )
  nil
end