#! /usr/bin/env ruby # coding: utf-8 # # # class Madowu::HtmlGenerator DEFAULT_MARKDOWN_COMMAND = 'markdown' # def initialize(md_file, markdown = DEFAULT_MARKDOWN_COMMAND) @md_file = md_file @markdown = markdown || DEFAULT_MARKDOWN_COMMAND @markup_lines = `#{@markdown} #{@md_file}`.split("\n") end #unless ARGV.size == 1 # PRINTUSAGE; # exit #end def self.get_title(md_file) #`grep -E '^#' #{md_file} | head -n 1`.sub(/^#\s*/, '').strip result = '' str = `head -n 1 #{md_file}` pattern = /^%\s*/ result = str.sub(/^%\s*/, '').chomp if pattern =~ str result end # 'title' element in head is set as first /^#/ in md_file. def generate(options = {}) embed_outline(options[:selflink]) if options[:outline] #if options[:dirmap] # md_dir = Pathname.new(@md_file).dirname.expand_path # embed_sidebar( Madowu::DirectoryMapper.dirmap(md_dir) ) #end if options[:sidebar] #lines = File.open( options[:sidebar], "r").readlines lines = `#{@markdown} #{options[:sidebar]}`.split("\n") embed_sidebar(lines) end result = [ make_header(options[:css], options[:charset]), @markup_lines, "", '' ].join("\n") end private def embed_sidebar(lines) lines.unshift "" @markup_lines += lines end def embed_outline(option_selflink = false) new_lines = [] outlines = [] anchor_index = 0 @markup_lines.each do |line| #pp line new_line = line if /^\(.*)\<\/h(\d)\>$/ =~ line new_line = '' new_line += "(.*)\<\/h(\d)\>$/ =~ line # new_line = '' # new_line += "", "

Outline:

") outlines.push "" @markup_lines = outlines + new_lines #new_lines = [] #outlines = [] #outlines << "
" #@markup_lines = outlines + new_lines end def make_header(css, charset) #title = `grep -E '^#' #{@md_file} | head -n 1`.sub(/^#\s*/, '').strip title = self.class.get_title(@md_file) charset ||= 'us-ascii' results = [] results << "" results << "" results << "" results << " #{title}" results << " " if css if File.exist? css md_dir = Pathname.new(@md_file).dirname.expand_path css_path = Pathname.new(css).expand_path rel_path = css_path.relative_path_from(md_dir) #pp md_dir #pp css_path #pp rel_path results << " " end end results << "" results << "" results << "
" results << "
" end end