Class: MarkdownComposer::DocumentIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_composer/document_index.rb,
lib/markdown_composer/document_index/html_parser.rb,
lib/markdown_composer/document_index/markdown_parser.rb

Defined Under Namespace

Classes: HtmlParser, MarkdownParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_key:, root:, nodes:, sections:, diagnostics:, lines:) ⇒ DocumentIndex

Returns a new instance of DocumentIndex.



78
79
80
81
82
83
84
85
# File 'lib/markdown_composer/document_index.rb', line 78

def initialize(source_key:, root:, nodes:, sections:, diagnostics:, lines:)
  @source_key = source_key
  @root = root
  @nodes = nodes
  @sections = sections
  @diagnostics = diagnostics
  @lines = lines
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



54
55
56
# File 'lib/markdown_composer/document_index.rb', line 54

def diagnostics
  @diagnostics
end

#linesObject (readonly)

Returns the value of attribute lines.



54
55
56
# File 'lib/markdown_composer/document_index.rb', line 54

def lines
  @lines
end

#nodesObject (readonly)

Returns the value of attribute nodes.



54
55
56
# File 'lib/markdown_composer/document_index.rb', line 54

def nodes
  @nodes
end

#rootObject (readonly)

Returns the value of attribute root.



54
55
56
# File 'lib/markdown_composer/document_index.rb', line 54

def root
  @root
end

#sectionsObject (readonly)

Returns the value of attribute sections.



54
55
56
# File 'lib/markdown_composer/document_index.rb', line 54

def sections
  @sections
end

#source_keyObject (readonly)

Returns the value of attribute source_key.



54
55
56
# File 'lib/markdown_composer/document_index.rb', line 54

def source_key
  @source_key
end

Class Method Details

.build(source, diagnostics: Diagnostics.new) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/markdown_composer/document_index.rb', line 56

def self.build(source, diagnostics: Diagnostics.new)
  source = Source.build(source)
  if source.format == :html
    from_html(source, diagnostics: diagnostics)
  else
    from_markdown(source.markdown.to_s, source_key: source.key, diagnostics: diagnostics)
  end
end

.from_html(source, diagnostics:) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/markdown_composer/document_index.rb', line 69

def self.from_html(source, diagnostics:)
  if defined?(Nokogiri)
    HtmlParser.new(source, diagnostics: diagnostics).index
  else
    diagnostics.error("source.html_parser_missing", "HTML input requires nokogiri", path: "sources.#{source.key}")
    from_markdown(source.html.to_s, source_key: source.key, diagnostics: diagnostics)
  end
end

.from_markdown(markdown, source_key: "buffer", diagnostics: Diagnostics.new) ⇒ Object



65
66
67
# File 'lib/markdown_composer/document_index.rb', line 65

def self.from_markdown(markdown, source_key: "buffer", diagnostics: Diagnostics.new)
  MarkdownParser.new(markdown.to_s, source_key: source_key, diagnostics: diagnostics).index
end

Instance Method Details

#markdown_for_range(start_line, end_line) ⇒ Object



87
88
89
# File 'lib/markdown_composer/document_index.rb', line 87

def markdown_for_range(start_line, end_line)
  lines[(start_line - 1)..(end_line - 1)].join
end