lib/giblish/indexheadings.rb in giblish-0.6.1 vs lib/giblish/indexheadings.rb in giblish-0.7.0

- old
+ new

@@ -39,11 +39,22 @@ # Use a class-global heading_index dict since asciidoctor creates a new instance # of this class for each processed file @heading_index = {"file_infos" => []} + # prio order: + # 1. values in this hash + # 2. values taken from the document + # 3. default values + @id_elements = { +# prefix: "_", +# separator: "_" + } + class << self + attr_accessor :id_elements + def heading_index @heading_index end def clear_index @@ -65,11 +76,11 @@ .relative_path_from(base_dir) end end Giblog.logger.info { "writing json to #{dst_dir.join("heading_index.json").to_s}" } - File.open(dst_dir.join("heading_index.json").to_s,"w") do |f| + File.open(dst_dir.join("heading_index.json").to_s, "w") do |f| f.write(heading_index.to_json) end end end @@ -85,29 +96,33 @@ # 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 = set_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) + index_sections(reader, file_info_hash, opts) heading_index["file_infos"] << file_info_hash reader end private # build the 'sections' array - def index_sections(reader, file_info_hash) + def index_sections(reader, file_info_hash, opts) sections = file_info_hash["sections"] heading_regex = Regexp.new(/^=+\s+(.*)$/) anchor_regex = Regexp.new(/^\[\[(\w+)\]\]\s*$/) @@ -135,21 +150,21 @@ end when :expecting_heading m = heading_regex.match(line) if m # we have an anchor and got a heading as expected, index it - section = { "id" => match_str } + section = {"id" => match_str} section["title"] = m[1].strip section["line_no"] = line_no sections << section else - Giblog.logger.debug {"Did not index the anchor: #{match_str} at line #{line_no}, probably not associated with a heading."} + Giblog.logger.debug { "Did not index the anchor: #{match_str} at line #{line_no}, probably not associated with a heading." } end state = :text when :heading # we got a heading without an accompanying anchor, index it - section = { "id" => get_unique_id(file_info_hash, match_str) } + section = {"id" => get_unique_id(file_info_hash, match_str, opts)} section["title"] = m[1].strip section["line_no"] = line_no sections << section state = :text end @@ -166,12 +181,50 @@ end end title end - def get_unique_id(doc_heading_dict, heading_str) - id_base = Giblish.to_valid_id(heading_str) + # id elements prio: + # 1. values in class variable + # 2. values taken from doc + # 3. default values + def set_id_attributes(lines) + # default values + result = { + id_prefix: "_", + id_separator: "_" + } + + # check if the doc specifies id attributes + Giblish.process_header_lines(lines) do |line| + m = /^:idprefix:(.*)$/.match(line) + n = /^:idseparator:(.*)$/.match(line) + if m + # We found a id prefix + result[:id_prefix] = m[1].strip + end + if n + # We found a id separator + result[:id_separator] = n[1].strip + end + end + + + if IndexHeadings.id_elements.has_key?(:id_prefix) + result[:id_prefix] = IndexHeadings.id_elements[:id_prefix] + end + + if IndexHeadings.id_elements.has_key?(:id_separator) + result[:id_separator] = IndexHeadings.id_elements[:id_separator] + end + + result + end + + def get_unique_id(doc_heading_dict, heading_str, opts) + + id_base = Giblish.to_valid_id(heading_str, opts[:id_prefix], opts[:id_separator]) return id_base if !doc_heading_dict.key? id_base # handle the case with several sections with the same name idx = 1 heading_id = "" @@ -188,15 +241,15 @@ def heading_index self.class.heading_index end end - # Helper method to register the docid preprocessor extension with # the asciidoctor engine. def register_index_heading_extension Asciidoctor::Extensions.register do preprocessor IndexHeadings end end + module_function :register_index_heading_extension end