Class: Cheepub::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/cheepub/markdown.rb

Constant Summary collapse

RUBY_PATTERN =
/{([^}]+)\|([^}]+)}/
TCY_PATTERN =

u005e is “^”

/\^([\u0020-\u005d\u005f\u007e]+)\^/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, **params) ⇒ Markdown

Returns a new instance of Markdown.



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

def initialize(text, **params)
  default_params = {template: File.join(Cheepub::TEMPLATES_DIR, "bodymatter.html.erb"),
                    lang: "ja",
                    title: "content",
                    cssfile: "style.css",
                    syntax_highlighter: nil,
                    generator: "Cheepub #{Cheepub::VERSION} with kramdown #{Kramdown::VERSION}",
                    input: "cheemarkdown",
                   }
  @params = default_params.merge(params)
  @text = text
end

Class Method Details

.parse(filename, **params) ⇒ Object



9
10
11
# File 'lib/cheepub/markdown.rb', line 9

def self.parse(filename, **params)
  Markdown.new(File.read(filename), **params)
end

Instance Method Details

#convertObject Also known as: to_html



26
27
28
29
30
# File 'lib/cheepub/markdown.rb', line 26

def convert
  params = @params.dup
  params[:syntax_highlighter] = "rouge"
  Kramdown::Document.new(@text, params).to_html
end

#save_as(filename) ⇒ Object



53
54
55
56
# File 'lib/cheepub/markdown.rb', line 53

def save_as(filename)
  html = convert
  File.write(filename, html)
end

#to_hash_astObject



41
42
43
44
45
# File 'lib/cheepub/markdown.rb', line 41

def to_hash_ast
  params = @params.dup
  params[:template] = nil
  Kramdown::Document.new(@text, params).to_hash_ast
end

#to_jsonObject



47
48
49
50
51
# File 'lib/cheepub/markdown.rb', line 47

def to_json
  params = @params.dup
  params[:template] = nil
  Kramdown::Document.new(@text, params).to_hash_ast.to_json
end

#to_latexObject



32
33
34
35
36
37
# File 'lib/cheepub/markdown.rb', line 32

def to_latex
  params = @params.dup
  params[:template] = File.join(Cheepub::TEMPLATES_DIR, "bodymatter.tex.erb")
  params[:latex_headers] = %w{chapter* section* subsection* subsubsection* paragraph* subparagraph*}
  Kramdown::Document.new(@text, params).to_latex
end