Class: Docwatch::Renderer::Markdown

Inherits:
Docwatch::Renderer show all
Defined in:
lib/docwatch/renderer/markdown.rb

Defined Under Namespace

Classes: HtmlRenderer

Instance Method Summary collapse

Methods inherited from Docwatch::Renderer

by_filetype, extension, #initialize, #js, #to_html

Constructor Details

This class inherits a constructor from Docwatch::Renderer

Instance Method Details

#bodyObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/docwatch/renderer/markdown.rb', line 36

def body
    # https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
    markdown_args = {
        fenced_code_blocks: true,
        disable_indented_code_blocks: true,
        tables: true,
        no_intra_emphasis: true,
        strikethrough: true,
        space_after_headers: true,
        superscript: true,
        highlight: true,
        footnotes: true,
    }

    # https://github.com/vmg/redcarpet#darling-i-packed-you-a-couple-renderers-for-lunch
    html_args = {
        with_toc_data: true,
        link_attributes: { rel: 'noreferrer nofollow noopener' },
        hard_wrap: true, # no need for trailing spaces to create new paragraphs
    }

    frontmatter, document = Parser::Frontmatter.split(contents)

    if frontmatter
        document = frontmatter.to_html + document
    end

    Redcarpet::Markdown.new(
        HtmlRenderer.new(html_args),
        markdown_args,
    ).render(document)
end

#cssObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/docwatch/renderer/markdown.rb', line 14

def css
    return default_css if @default_styles

    config_dir = ENV.fetch('XDG_CONFIG_HOME', File.expand_path('~/.config'))
    styles_path = File.join(config_dir, 'docwatch', 'styles.css')

    if File.exist?(styles_path)
        File.read(styles_path)
    else
        default_css
    end
end

#default_cssObject



10
11
12
# File 'lib/docwatch/renderer/markdown.rb', line 10

def default_css
    File.read(Docwatch.root_dir + '/res/styles.css')
end

#headObject



27
28
29
30
31
32
33
34
# File 'lib/docwatch/renderer/markdown.rb', line 27

def head
    return <<~EOF
        <title>#{file_path} — docwatch</title>
        <style>
        #{css}
        </style>
    EOF
end