Class: Giblish::DocidCollector

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

Overview

Parse all adoc files for :docid: attributes

Constant Summary collapse

ID_MIN_LENGTH =

The minimum number of characters required for a valid doc id

2
ID_MAX_LENGTH =

The maximum number of characters required for a valid doc id

10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_cacheObject



18
19
20
# File 'lib/giblish/docid.rb', line 18

def clear_cache
  @docid_cache = {}
end

.docid_cacheObject



14
15
16
# File 'lib/giblish/docid.rb', line 14

def docid_cache
  @docid_cache
end

Instance Method Details

#parse_file(path) ⇒ Object

Check if a :docid: <id> entry exists in the header. According to www.methods.co.nz/asciidoc/userguide.html#X95 the header is optional, but if it exists it:

  • must start with a titel (=+ <My Title>)

  • ends with one or more blank lines

  • does not contain any blank line



65
66
67
68
69
70
71
72
73
74
# File 'lib/giblish/docid.rb', line 65

def parse_file(path)
  Giblog.logger.debug { "parsing file #{path} for docid..." }
  process_header_lines(path) do |line|
    m = /^:docid: +(.*)$/.match(line)
    if m
      # There is a docid defined, cache the path and doc id
      validate_and_add m[1], path
    end
  end
end

#process(document, reader) ⇒ Object

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

It replaces references of the format <<:docid: ID-1234,Hello >> with references to a resolved relative path.



81
82
83
84
85
86
87
88
# File 'lib/giblish/docid.rb', line 81

def process(document, reader)
  reader.lines.each do |line|
    line.gsub!(/<<\s*:docid:\s*(.*)>>/) do |_m|
      replace_doc_id Regexp.last_match(1), document.attributes["docfile"]
    end
  end
  reader
end

#process_header_lines(path) ⇒ Object

Helper method that provides the user with a way of processing only the lines within the asciidoc header block. The user must return nil to get the next line.

ex: process_header_lines(file_path) do |line|

if line == "Quack!"
   puts "Donald!"
   1
else
   nil
end

end



48
49
50
51
52
53
54
55
56
57
# File 'lib/giblish/docid.rb', line 48

def process_header_lines(path)
  state = "before_header"
  File.foreach(path) do |line|
    case state
    when "before_header" then (state = "in_header" if line =~ /^=+.*$/)
    when "in_header" then (state = "done" if line =~ /^\s*$/ || yield(line))
    when "done" then break
    end
  end
end

#substitute_ids(src_str, src_path) ⇒ Object



94
95
96
97
98
99
# File 'lib/giblish/docid.rb', line 94

def substitute_ids(src_str, src_path)
  src_str.gsub!(/<<\s*:docid:\s*(.*)>>/) do |_m|
    replace_doc_id Regexp.last_match(1), src_path
  end
  src_str
end

#substitute_ids_file(path) ⇒ Object



90
91
92
# File 'lib/giblish/docid.rb', line 90

def substitute_ids_file(path)
  substitute_ids(File.read(path), path)
end