Class: RedQuilt::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/red_quilt/document.rb,
sig/red_quilt.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, arena, root_id, allow_html: false, disallow_raw_html: false, references: {}, footnotes: nil, frontmatter: nil) ⇒ Document

Returns a new instance of Document.



7
8
9
10
11
12
13
14
15
16
# File 'lib/red_quilt/document.rb', line 7

def initialize(source, arena, root_id, allow_html: false, disallow_raw_html: false, references: {}, footnotes: nil, frontmatter: nil)
  @source = source
  @arena = arena
  @root_id = root_id
  @allow_html = allow_html
  @disallow_raw_html = disallow_raw_html
  @references = references
  @footnotes = footnotes
  @frontmatter = frontmatter
end

Instance Attribute Details

#arenaArena (readonly)

Returns the value of attribute arena.

Returns:



5
6
7
# File 'lib/red_quilt/document.rb', line 5

def arena
  @arena
end

#footnotesFootnoteRegistry? (readonly)

Returns the value of attribute footnotes.

Returns:



5
6
7
# File 'lib/red_quilt/document.rb', line 5

def footnotes
  @footnotes
end

#frontmatterObject (readonly)

Returns the value of attribute frontmatter.



5
6
7
# File 'lib/red_quilt/document.rb', line 5

def frontmatter
  @frontmatter
end

#referencesHash[String, Hash[Symbol, untyped]] (readonly)

Returns the value of attribute references.

Returns:

  • (Hash[String, Hash[Symbol, untyped]])


5
6
7
# File 'lib/red_quilt/document.rb', line 5

def references
  @references
end

#root_idInteger (readonly)

Returns the value of attribute root_id.

Returns:

  • (Integer)


5
6
7
# File 'lib/red_quilt/document.rb', line 5

def root_id
  @root_id
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


5
6
7
# File 'lib/red_quilt/document.rb', line 5

def source
  @source
end

Instance Method Details

#allow_html?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/red_quilt/document.rb', line 18

def allow_html?
  @allow_html
end

#diagnosticsArray[Diagnostic]

Returns the array of diagnostics collected during parse / render. The array is mutable and shared with the parser / renderer; new entries appear here without further calls.

Returns:



96
97
98
# File 'lib/red_quilt/document.rb', line 96

def diagnostics
  @diagnostics ||= []
end

#disallow_raw_html?Boolean

When true, raw HTML output filters the 9 dangerous tags defined by GFM's "Disallowed Raw HTML" extension (title, textarea, style, xmp, iframe, noembed, noframes, script, plaintext) by replacing their leading < with &lt;. Only meaningful when allow_html? is true; when allow_html? is false everything is already escaped.

Returns:

  • (Boolean)


27
28
29
# File 'lib/red_quilt/document.rb', line 27

def disallow_raw_html?
  @disallow_raw_html
end

#first_heading_textString?

Returns the plain-text content of the first HEADING in the document, or nil if there is no heading. Used by callers (e.g. the CLI's --auto-title) to derive a document title.

Returns:

  • (String, nil)


85
86
87
# File 'lib/red_quilt/document.rb', line 85

def first_heading_text
  first_heading_text_walk(@root_id)
end

#rootNodeRef

Returns:



31
32
33
# File 'lib/red_quilt/document.rb', line 31

def root
  NodeRef.new(self, @root_id)
end

#source_mapSourceMap

Returns:



89
90
91
# File 'lib/red_quilt/document.rb', line 89

def source_map
  @source_map ||= SourceMap.new(@source)
end

#to_astHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


69
70
71
# File 'lib/red_quilt/document.rb', line 69

def to_ast
  root.to_h
end

#to_html(standalone: false, title: nil, lang: nil, css: nil, theme: :none, heading_ids: false, mermaid: false) ⇒ Object

Renders the document to HTML.

standalone: when true, wrap the rendered body in a <!DOCTYPE html> template with <head> (charset / title / optional stylesheet) and <body>. When false (the default), only the rendered body fragment is returned. title / lang / css / theme: applied only when standalone is true. theme: a bundled stylesheet to inline (:none embeds nothing, keeping the bare template; :default embeds RedQuilt's default theme). css (an external stylesheet link) is independent and may be combined. heading_ids: when true, every heading gets a slugified id (Unicode preserving, deduplicated within the document) for anchor links. mermaid: when true, fenced code blocks tagged mermaid render as <pre class="mermaid"> containers instead of <pre><code>. In standalone mode the mermaid.js runtime is also loaded from a CDN so the diagrams render in the browser without further setup.

When standalone and the document was parsed with frontmatter: true, the frontmatter's title / lang keys fill in the corresponding <title> / <html lang> if no explicit argument was given (explicit argument > frontmatter > default).



60
61
62
63
64
65
66
67
# File 'lib/red_quilt/document.rb', line 60

def to_html(standalone: false, title: nil, lang: nil, css: nil, theme: :none, heading_ids: false, mermaid: false)
  body = Renderer::HTML.new(self, heading_ids: heading_ids, mermaid: mermaid).render
  return body unless standalone

  effective_title = title || frontmatter_value("title")
  effective_lang = lang || frontmatter_value("lang") || "en"
  wrap_standalone_html(body, title: effective_title.to_s, lang: effective_lang.to_s, css: css, theme: Theme.css(theme), mermaid: mermaid)
end

#to_jsonString

Parameters:

  • (Object)

Returns:

  • (String)


73
74
75
76
# File 'lib/red_quilt/document.rb', line 73

def to_json(*)
  require "json"
  JSON.pretty_generate(to_mdast)
end

#to_mdastHash[String, untyped]

Returns:

  • (Hash[String, untyped])


78
79
80
# File 'lib/red_quilt/document.rb', line 78

def to_mdast
  Renderer::Mdast.new(self).render
end

#walkvoid #walkEnumerator[NodeRef, void]

Overloads:

  • #walkvoid

    This method returns an undefined value.

  • #walkEnumerator[NodeRef, void]

    Returns:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


35
36
37
# File 'lib/red_quilt/document.rb', line 35

def walk(&)
  root.walk(&)
end