Class: Apex::Document

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

Overview

High-level document API for Apex Markdown.

The primary entry point is Document.markdown_to_html, which mirrors the style of Kramdown::Document.new(text, options).to_html but uses a single convenient class method:

html = Apex::Document.markdown_to_html(text, mode: :unified)

You can also use the instance-level API if you prefer object instances that you can reuse:

doc = Apex::Document.new(text, mode: :gfm, enable_tables: true)
html = doc.to_html

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, mode: :unified, **options) ⇒ Document

Create a new document instance.

Parameters:

  • source (#to_s)

    the Markdown source text

  • mode (Symbol, String) (defaults to: :unified)

    processing mode, defaults to :unified

  • options (Hash)

    additional Apex options



52
53
54
55
56
# File 'lib/apex/document.rb', line 52

def initialize(source, mode: :unified, **options)
  @source  = String(source)
  @mode    = mode
  @options = options
end

Instance Attribute Details

#modeSymbol, String (readonly)

Returns the selected Apex mode.

Returns:

  • (Symbol, String)

    the selected Apex mode



42
43
44
# File 'lib/apex/document.rb', line 42

def mode
  @mode
end

#optionsHash (readonly)

Returns options passed on to the native Apex renderer.

Returns:

  • (Hash)

    options passed on to the native Apex renderer



45
46
47
# File 'lib/apex/document.rb', line 45

def options
  @options
end

#sourceString (readonly)

Returns the original Markdown source.

Returns:

  • (String)

    the original Markdown source



39
40
41
# File 'lib/apex/document.rb', line 39

def source
  @source
end

Class Method Details

.markdown_to_html(source, mode: :unified, **options) ⇒ String

Render Markdown to HTML using Apex.

Parameters:

  • source (#to_s)

    the Markdown source text

  • mode (Symbol, String) (defaults to: :unified)

    processing mode (:unified by default). Accepted values:

    • :unified

    • :gfm, :github

    • :multimarkdown, :mmd

    • :commonmark, :cmark

    • :kramdown

  • options (Hash)

    additional Apex options, mapped directly to the underlying apex_options struct (see C header for details).

Returns:



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

def self.markdown_to_html(source, mode: :unified, **options)
  opts = { mode: mode }.merge(options)
  Apex::Native.markdown_to_html(String(source), opts)
end

Instance Method Details

#to_htmlString

Render this document to HTML.

Returns:



61
62
63
# File 'lib/apex/document.rb', line 61

def to_html
  self.class.markdown_to_html(@source, mode: @mode, **@options)
end