class Markun::Core

Markun Core

Constants

HTML_TEMPLATE
MARKUN_FILE
MARKUN_TEMPLATE

Public Instance Methods

execute() click to toggle source

execute markdown convert

# File lib/markun_core.rb, line 42
def execute
  src = read_dsl
  dsl = Markun::Dsl.new
  dsl.instance_eval src
  convert_markdown_to_html dsl.markun.have_menu
end
init() click to toggle source

generate Markunfile to current directory.

# File lib/markun_core.rb, line 37
def init
  File.open(MARKUN_FILE, 'w') { |f|f.puts MARKUN_TEMPLATE }
end

Private Instance Methods

convert_markdown_to_html(have_menu) click to toggle source
# File lib/markun_core.rb, line 55
def convert_markdown_to_html(have_menu)
  Dir.glob('**/*.md').each do |file|
    md = File.read(file)
    contents = Kramdown::Document.new(md.force_encoding('utf-8')).to_html
    menu = get_menu(file, have_menu)
    html = get_html_template(File.basename(file, '.md'), contents, menu)
    html_file_name = file.gsub('.md', '.html')
    File.open(html_file_name, 'w:utf-8') { |f|f.puts html.encode('utf-8') }
  end
end
create_menu(base) click to toggle source
# File lib/markun_core.rb, line 78
def create_menu(base)
  urls = get_urls([])
  urls_to_menu(urls, base)
end
get_each_urls() click to toggle source
# File lib/markun_core.rb, line 93
def get_each_urls
  Dir.glob('./*.md').map { |f|File.absolute_path(f) }
end
get_html_template(title, contents, menu) click to toggle source
# File lib/markun_core.rb, line 73
def get_html_template(title, contents, menu)
  erb = ERB.new(HTML_TEMPLATE)
  erb.result(binding)
end
get_menu(file, have_menu) click to toggle source
# File lib/markun_core.rb, line 66
def get_menu(file, have_menu)
  return '' unless have_menu == 'true'
  absolute_path = File.dirname(File.absolute_path(file))
  base = Pathname.new(absolute_path)
  create_menu base
end
get_urls(urls) click to toggle source
# File lib/markun_core.rb, line 83
def get_urls(urls)
  urls += get_each_urls
  Dir.glob('*/') do |d|
    Dir.chdir(d)
    urls = get_urls(urls)
    Dir.chdir('../')
  end
  urls
end
read_dsl() click to toggle source
# File lib/markun_core.rb, line 51
def read_dsl
  File.open(MARKUN_FILE) { |f|f.read }
end
urls_to_menu(urls, base) click to toggle source
# File lib/markun_core.rb, line 97
def urls_to_menu(urls, base)
  ret = []
  urls.each do |f|
    _filename = Pathname.new(f.gsub('md', 'html')).relative_path_from(base)
    ret << "<a href='#{_filename}'>#{_filename}</a><br />"
  end
  ret.join('') + '<hr />'
end