Class: Md2Tex

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
lib/md2tex.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Md2Tex

Returns a new instance of Md2Tex.



5
6
7
8
# File 'lib/md2tex.rb', line 5

def initialize options = {}
  @sectionsuffix = options[:section_numbers] ? '' : '*'
  super()
end

Class Method Details

.load_templatesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/md2tex.rb', line 72

def self.load_templates
  @@templates = {}

  begin
    io = File.read(__FILE__).force_encoding(Encoding::UTF_8)
    app, data = io.gsub("\r\n", "\n").split(/^__END__$/, 2)
  rescue Errno::ENOENT
    app, data = nil
  end

  if data
    template = nil
    data.each_line do |line|
      if line =~ /^@@\s*(.*\S)\s*$/
        template = ''
        @@templates[$1.to_sym] = template
      elsif template
        template << line
      end
    end
  end
end

.process(filename, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/md2tex.rb', line 97

def self.process filename, options = {}
  content = File.read(filename).force_encoding(Encoding::UTF_8)
  md = Redcarpet::Markdown.new(Md2Tex.new(options), fenced_code_blocks: true)
  tex = md.render(content)
  texfile = "#{File.basename(filename, '.md')}.tex"
  Dir.chdir File.dirname(filename) do
    File.write(texfile, tex)
    system 'xelatex', texfile
  end
end

Instance Method Details

#block_code(code, lang) ⇒ Object



60
61
62
# File 'lib/md2tex.rb', line 60

def block_code code, lang
  "\\begin{verbatim}\n#{code}\\end{verbatim}\n\n"
end

#block_quote(text) ⇒ Object



32
33
34
# File 'lib/md2tex.rb', line 32

def block_quote text
  "\\begin{quote}\n#{text}\\end{quote}\n\n"
end

#codespan(code) ⇒ Object



56
57
58
# File 'lib/md2tex.rb', line 56

def codespan code
  "\\texttt{#{code}}"
end


68
69
70
# File 'lib/md2tex.rb', line 68

def doc_footer
  @@templates[:footer]
end

#doc_headerObject



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

def doc_header
  @@templates[:header]
end

#double_emphasis(text) ⇒ Object



48
49
50
# File 'lib/md2tex.rb', line 48

def double_emphasis text
  "\\textbf{#{text}}"
end

#emphasis(text) ⇒ Object



44
45
46
# File 'lib/md2tex.rb', line 44

def emphasis text
  "\\textit{#{text}}"
end

#header(h, level) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/md2tex.rb', line 10

def header h, level
  case level
  when 1 then p "\\section#{@sectionsuffix}{#{h}}\n\n"
  when 2 then "\\subsection#{@sectionsuffix}{#{h}}\n\n"
  when 3 then "\\subsubsection#{@sectionsuffix}{#{h}}\n\n"
  else h
  end
end

#hruleObject



36
37
38
# File 'lib/md2tex.rb', line 36

def hrule
  "\\centerline{—}\n\n"
end

#linebreakObject



40
41
42
# File 'lib/md2tex.rb', line 40

def linebreak
  "\\\\\n"
end

#list(contents, list_type) ⇒ Object



23
24
25
26
# File 'lib/md2tex.rb', line 23

def list contents, list_type
  t = list_type == :ordered ? 'enumerate' : 'itemize'
  "\\begin{#{t}}\n#{contents}\\end{#{t}}\n\n"
end

#list_item(text, list_type) ⇒ Object



28
29
30
# File 'lib/md2tex.rb', line 28

def list_item text, list_type
  "\\item #{text}\n"
end

#paragraph(text) ⇒ Object



19
20
21
# File 'lib/md2tex.rb', line 19

def paragraph text
  "#{text}\n\n"
end

#triple_emphasis(text) ⇒ Object



52
53
54
# File 'lib/md2tex.rb', line 52

def triple_emphasis text
  "\\textbf{\\textit{#{text}}}"
end