Class: Mdpdf::Md2Pdf

Inherits:
Object
  • Object
show all
Defined in:
lib/mdpdf/md2pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(mdfile_name, outfile_name = nil) ⇒ Md2Pdf

Returns a new instance of Md2Pdf.



6
7
8
9
10
# File 'lib/mdpdf/md2pdf.rb', line 6

def initialize(mdfile_name,outfile_name = nil)
  @mdfile_name = mdfile_name
  @outfile_name = outfile_name
  file_read
end

Instance Method Details

#creat_md(html_body) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mdpdf/md2pdf.rb', line 22

def creat_md(html_body)
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :tables => true, :autolink => true,:fenced_code_blocks => true) 
  body = markdown.render(html_body) 
  html_template = "
    <!DOCTYP html>
    <html>
      <head>
        <meta charset='UTF-8'>
        <title>#{@outfile_name}</title>
        <link rel='stylesheet' type='text/css' href='github.css'>
      </head>
      <body>
        #{body}
      </body>
    </html>
  "
  create_pdf(html_template)
end

#create_pdf(html) ⇒ Object



41
42
43
44
45
# File 'lib/mdpdf/md2pdf.rb', line 41

def create_pdf(html)
  file = open("./#{@outfile_name}.pdf","w")
  file.puts PDFKit.new(html,encoding: "UTF-8").to_pdf
  file.close
end

#file_readObject



12
13
14
15
16
17
18
19
20
# File 'lib/mdpdf/md2pdf.rb', line 12

def file_read
  html_body = String.new
  File.open(@mdfile_name) do|file|
    file.read.split("\n").each do |c|
      html_body += c + "\n"
    end
  end
  creat_md(html_body)
end