Class: OCTool::SSP

Inherits:
Object
  • Object
show all
Defined in:
lib/octool/ssp.rb

Overview

Build DB, CSV, and markdown.

Instance Method Summary collapse

Constructor Details

#initialize(system, output_dir) ⇒ SSP

Returns a new instance of SSP.



8
9
10
11
# File 'lib/octool/ssp.rb', line 8

def initialize(system, output_dir)
    @system = system
    @output_dir = output_dir
end

Instance Method Details

#generateObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/octool/ssp.rb', line 25

def generate
    unless File.writable?(@output_dir)
        warn "[FAIL] #{@output_dir} is not writable"
        exit(1)
    end
    render_template
    write_acronyms
    write 'pdf'
    write 'docx'
end

#md_pathObject

rubocop:enable Metrics/AbcSize,Metrics/MethodLength



76
77
78
# File 'lib/octool/ssp.rb', line 76

def md_path
    @md_path ||= File.join(@output_dir, 'ssp.md')
end

#pandocObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/octool/ssp.rb', line 13

def pandoc
    @pandoc ||= begin
        # Load paru/pandoc late enough that help functions work
        # and early enough to be confident that we catch the correct error.
        require 'paru/pandoc'
        Paru::Pandoc.new
    end
rescue UncaughtThrowError
    warn '[FAIL] octool requires pandoc to generate the SSP. Is pandoc installed?'
    exit(1)
end

#render_templateObject



36
37
38
39
40
41
42
43
# File 'lib/octool/ssp.rb', line 36

def render_template
    print "Building markdown #{md_path} ... "
    template_path = File.join(ERB_DIR, 'ssp.erb')
    template = File.read(template_path)
    output = ERB.new(template, nil, '-').result(binding)
    File.open(md_path, 'w') { |f| f.puts output }
    puts 'done'
end

#write(type = 'pdf') ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/octool/ssp.rb', line 54

def write(type = 'pdf')
    out_path = File.join(@output_dir, "ssp.#{type}")
    print "Building #{out_path} ... "
    converter = pandoc.configure do
        from 'markdown'
        to type
        pdf_engine 'lualatex'
        toc
        toc_depth 3
        number_sections
        highlight_style 'pygments'
        filter 'pandoc-acronyms' if ENV['PANDOC_ACRONYMS_ACRONYMS']
        # https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Encoding_issue
        # Uncomment the following line after the "listings" package is compatible with utf8
        # listings
    end
    output = converter << File.read(md_path)
    File.new(out_path, 'wb').write(output)
    puts 'done'
end

#write_acronymsObject



45
46
47
48
49
50
51
# File 'lib/octool/ssp.rb', line 45

def write_acronyms
    return unless @system.acronyms

    out_path = File.join(@output_dir, 'acronyms.json')
    File.open(out_path, 'w') { |f| f.write JSON.pretty_generate(@system.acronyms) }
    ENV['PANDOC_ACRONYMS_ACRONYMS'] = out_path
end