Class: Giblish::IndexHeadings

Inherits:
Asciidoctor::Extensions::Preprocessor
  • Object
show all
Defined in:
lib/giblish/indexheadings.rb

Overview

This hook is called by Asciidoctor once for each document before Asciidoctor processes the adoc content.

It indexes all headings found in all documents in the tree. The resulting index can be serialized to a JSON file with the following format:

{

file_infos : [{
  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_1,
  ...
}]

}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.heading_indexObject (readonly)

Returns the value of attribute heading_index.


55
56
57
# File 'lib/giblish/indexheadings.rb', line 55

def heading_index
  @heading_index
end

.id_elementsObject

Returns the value of attribute id_elements.


53
54
55
# File 'lib/giblish/indexheadings.rb', line 53

def id_elements
  @id_elements
end

Class Method Details

.clear_indexObject


57
58
59
# File 'lib/giblish/indexheadings.rb', line 57

def clear_index
  @heading_index = { "file_infos" => [] }
end

.serialize(dst_dir, base_dir = "") ⇒ Object

write the index to a file in dst_dir and remove the base_dir part of the path for each filename


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

def serialize(dst_dir, base_dir = "")
  dst_dir = Pathname.new(dst_dir) unless dst_dir.respond_to?(:join)
  base_dir = Pathname.new(base_dir) unless base_dir.respond_to?(:join)

  if base_dir.to_s.empty?
    heading_index
  else
    # remove the base_dir part of the file path
    heading_index["file_infos"].each do |file_info|
      file_info["filepath"] = Pathname.new(file_info["filepath"])
                                      .relative_path_from(base_dir)
    end
  end

  Giblog.logger.info { "writing json to #{dst_dir.join('heading_index.json')}" }
  File.open(dst_dir.join("heading_index.json").to_s, "w") do |f|
    f.write(heading_index.to_json)
  end
end

Instance Method Details

#process(document, reader) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/giblish/indexheadings.rb', line 84

def process(document, reader)
  # Add doc as a source dependency for doc ids
  src_path = document.attributes["docfile"]

  # Note: the nil check is there to prevent us adding generated
  # asciidoc docs that does not exist in the file system (e.g. the
  # generated index pages). This is a bit hackish and should maybe be
  # done differently
  return if src_path.nil?

  # get the title from thw raw text (asciidoctor has not yet
  # processed the text)
  title = find_title reader.lines

  # make sure we use the correct id elements when indexing
  # sections
  opts = find_id_attributes(reader.lines)

  # Index all headings in the doc
  Giblog.logger.debug { "indexing headings in #{src_path}" }
  sections = []
  file_info_hash = {
    "filepath" => src_path,
    "title" => title,
    "sections" => sections
  }

  index_sections(reader, file_info_hash, opts)

  heading_index["file_infos"] << file_info_hash
  reader
end