Class: RedQuilt::Document

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

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

#arenaObject (readonly)

Returns the value of attribute arena.



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

def arena
  @arena
end

#footnotesObject (readonly)

Returns the value of attribute footnotes.



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

#referencesObject (readonly)

Returns the value of attribute references.



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

def references
  @references
end

#root_idObject (readonly)

Returns the value of attribute root_id.



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

def root_id
  @root_id
end

#sourceObject (readonly)

Returns the value of attribute source.



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

#diagnosticsObject

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.



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_textObject

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.



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

def first_heading_text
  first_heading_text_walk(@root_id)
end

#rootObject



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

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

#source_mapObject



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

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

#to_astObject



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_jsonObject



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

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

#to_mdastObject



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

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

#walkObject



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

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