Module: Gmd

Defined in:
lib/gmd.rb,
lib/gmd/helpers.rb,
lib/gmd/version.rb

Defined Under Namespace

Modules: Helpers Classes: ExtraBinding

Constant Summary collapse

VERSION =
"1.3.1"

Class Method Summary collapse

Class Method Details

.choose_file_from_paths(paths) ⇒ Object



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

def choose_file_from_paths(paths)
  paths.map { |p| File.expand_path(p) }.find { |p| File.file?(p) }
end

.default_layoutObject



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

def default_layout
  default_layouts_dir + "/default/layout.html.erb"
end

.default_layouts_dirObject



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

def default_layouts_dir
  File.dirname(__FILE__) + "/gmd/templates/layouts"
end

.escape_markdown(str) ⇒ Object

Escapes all markdown symbols found



63
64
65
66
67
# File 'lib/gmd.rb', line 63

def escape_markdown(str)
  symbols = "\\`*_{}[]()#+-.!".split('')
  symbols.each { |s| str.gsub!(s, "\\" + s) }
  str
end

.fix_latex(str) ⇒ Object

Used to find and autoescape LaTeX in Markdown files NOT SAFE YET! DO NO USE THIS!



71
72
73
74
75
76
77
# File 'lib/gmd.rb', line 71

def fix_latex(str)
  inline_exp = /(([^\$]\${1}[^\$].*?[^\$]?\${1}[^\$]))/
  multline_exp = /((\\begin\{(\w+?)\}.+?\\end\{\3\})|(\$\$.+?\$\$))/m
  str.gsub!(multline_exp) { |s| escape_markdown($1) }
  str.gsub!(inline_exp) { |s| escape_markdown($1) }
  str
end

.get_layout_paths(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gmd.rb', line 11

def get_layout_paths(file)
  [
    file,
    "#{file}.html.erb",
    "#{file}/layout.html.erb",
    "layout/#{file}",
    "layout/#{file}.html.erb",
    "layouts/#{file}",
    "layouts/#{file}.html.erb",
    "layouts/#{file}/layout.html.erb",
    "~/.gmd/#{file}",
    "~/.gmd/#{file}.html.erb",
    "~/.gmd/#{file}/layout.html.erb",
    File.join(default_layouts_dir, file),
    File.join(default_layouts_dir, file + ".html.erb"),
    File.join(default_layouts_dir, file + "/layout.html.erb"),
    "~/.tilt/#{file}",
    "/etc/tilt/#{file}"
  ]
end

.layout_meta_tagsObject



44
45
46
47
48
# File 'lib/gmd.rb', line 44

def layout_meta_tags
  <<-METATAGS
    <meta name="generator" content="gmd #{Gmd::VERSION}">
  METATAGS
end

.load_common(file) ⇒ Object

Used to load files from templates/common/ dir



51
52
53
54
55
56
57
58
59
60
# File 'lib/gmd.rb', line 51

def load_common(file)
  paths = [
    File.dirname(__FILE__) + "/gmd/templates/common/#{file}",
    File.dirname(__FILE__) + "/gmd/templates/common/#{file}.html.erb",
    File.dirname(__FILE__) + "/gmd/templates/common/#{file}.erb"
  ]
  filepath = choose_file_from_paths(paths)
  raise "Unable to find file: #{file}" unless filepath
  tilt_render(filepath)
end

.rendering_options(engine) ⇒ Object

Here we can specify the default options for different renderers



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gmd.rb', line 84

def rendering_options(engine)
  case engine.name
  when /Redcarpet/
    {
      :no_intra_emphasis => true,
      :tables => true,
      :fenced_code_blocks => true,
      :autolink => true,
      :strikethrough => true,
      :lax_html_blocks => true,
      :space_after_headers => true,
      :superscript => false,
      :renderer => Redcarpet::Render::SmartyHTML.new({
        :with_toc_data => true
      })
    }
  end
end

.tilt_render(file, locals = {}, &block) ⇒ Object



103
104
105
106
107
# File 'lib/gmd.rb', line 103

def tilt_render(file, locals = {}, &block)
  Tilt.new(file, 1, rendering_options(Tilt[file]) || {})
    .render(Gmd::ExtraBinding, locals, &block)
    .force_encoding("UTF-8")
end

.varObject



79
80
81
# File 'lib/gmd.rb', line 79

def var
  @var ||= {}
end