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) ⇒ Document

Returns a new instance of Document.



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

def initialize(source, arena, root_id, allow_html: false, disallow_raw_html: false, references: {}, footnotes: nil)
  @source = source
  @arena = arena
  @root_id = root_id
  @allow_html = allow_html
  @disallow_raw_html = disallow_raw_html
  @references = references
  @footnotes = footnotes
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

#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)


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

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.



82
83
84
# File 'lib/red_quilt/document.rb', line 82

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)


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

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.



71
72
73
# File 'lib/red_quilt/document.rb', line 71

def first_heading_text
  first_heading_text_walk(@root_id)
end

#rootObject



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

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

#source_mapObject



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

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

#to_astObject



55
56
57
# File 'lib/red_quilt/document.rb', line 55

def to_ast
  root.to_h
end

#to_html(standalone: false, title: nil, lang: "en", css: nil, theme: :none) ⇒ 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.


48
49
50
51
52
53
# File 'lib/red_quilt/document.rb', line 48

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

  wrap_standalone_html(body, title: title.to_s, lang: lang.to_s, css: css, theme: Theme.css(theme))
end

#to_jsonObject



59
60
61
62
# File 'lib/red_quilt/document.rb', line 59

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

#to_mdastObject



64
65
66
# File 'lib/red_quilt/document.rb', line 64

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

#walkObject



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

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