Class: RedQuilt::Document
- Inherits:
-
Object
- Object
- RedQuilt::Document
- Defined in:
- lib/red_quilt/document.rb
Instance Attribute Summary collapse
-
#arena ⇒ Object
readonly
Returns the value of attribute arena.
-
#footnotes ⇒ Object
readonly
Returns the value of attribute footnotes.
-
#frontmatter ⇒ Object
readonly
Returns the value of attribute frontmatter.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
-
#root_id ⇒ Object
readonly
Returns the value of attribute root_id.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #allow_html? ⇒ Boolean
-
#diagnostics ⇒ Object
Returns the array of diagnostics collected during parse / render.
-
#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 `<`.
-
#first_heading_text ⇒ Object
Returns the plain-text content of the first HEADING in the document, or nil if there is no heading.
-
#initialize(source, arena, root_id, allow_html: false, disallow_raw_html: false, references: {}, footnotes: nil, frontmatter: nil) ⇒ Document
constructor
A new instance of Document.
- #root ⇒ Object
- #source_map ⇒ Object
- #to_ast ⇒ Object
-
#to_html(standalone: false, title: nil, lang: nil, css: nil, theme: :none, heading_ids: false, mermaid: false) ⇒ Object
Renders the document to HTML.
- #to_json ⇒ Object
- #to_mdast ⇒ Object
- #walk ⇒ Object
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
#arena ⇒ Object (readonly)
Returns the value of attribute arena.
5 6 7 |
# File 'lib/red_quilt/document.rb', line 5 def arena @arena end |
#footnotes ⇒ Object (readonly)
Returns the value of attribute footnotes.
5 6 7 |
# File 'lib/red_quilt/document.rb', line 5 def footnotes @footnotes end |
#frontmatter ⇒ Object (readonly)
Returns the value of attribute frontmatter.
5 6 7 |
# File 'lib/red_quilt/document.rb', line 5 def frontmatter @frontmatter end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
5 6 7 |
# File 'lib/red_quilt/document.rb', line 5 def references @references end |
#root_id ⇒ Object (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 |
#source ⇒ Object (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
18 19 20 |
# File 'lib/red_quilt/document.rb', line 18 def allow_html? @allow_html end |
#diagnostics ⇒ Object
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 `<`. Only meaningful when allow_html? is true; when allow_html? is false everything is already escaped.
27 28 29 |
# File 'lib/red_quilt/document.rb', line 27 def disallow_raw_html? @disallow_raw_html end |
#first_heading_text ⇒ Object
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 |
#root ⇒ Object
31 32 33 |
# File 'lib/red_quilt/document.rb', line 31 def root NodeRef.new(self, @root_id) end |
#source_map ⇒ Object
89 90 91 |
# File 'lib/red_quilt/document.rb', line 89 def source_map @source_map ||= SourceMap.new(@source) end |
#to_ast ⇒ Object
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_json ⇒ Object
73 74 75 76 |
# File 'lib/red_quilt/document.rb', line 73 def to_json(*) require "json" JSON.pretty_generate(to_mdast) end |
#to_mdast ⇒ Object
78 79 80 |
# File 'lib/red_quilt/document.rb', line 78 def to_mdast Renderer::Mdast.new(self).render end |
#walk ⇒ Object
35 36 37 |
# File 'lib/red_quilt/document.rb', line 35 def walk(&) root.walk(&) end |