class Mok::Mok2Html

Constants

RELEASE
VERSION

Public Class Methods

new(src, options = {}) click to toggle source
# File lib/mok2html.rb, line 12
def initialize(src, options = {})
  @debug = true

  # options
  @css        = files_to_str(options[:css])
  @javascript = files_to_str(options[:javascript])
  @language   = options[:language]
  @index      = options[:index]
  @metadata   = options[:metadata]
  @quiet      = options[:quiet]
  options[:variables] = get_variables(files_to_str(options[:variable], true))
  get_customized_element(options[:custom_element]) unless options[:custom_element].empty?

  @mok = BlockParser.new(options)
  @metadata = setup_metadata
  @nodes = @mok.parse src
end

Public Instance Methods

body() click to toggle source
# File lib/mok2html.rb, line 72
def body
  @nodes.map do |node| node.apply end.join
end
css() click to toggle source
# File lib/mok2html.rb, line 161
def css
  str = ""
  str += %[<style type="text/css"><!--\n#{@css}\n--></style>\n] unless @css.empty?
  str
end
files_to_str(files = [], ignore = false) click to toggle source

複数ファイルの文字列を返す files: ファイルの配列または“,”区切り文字列 ignore: ファイルが存在しない時に無視する

# File lib/mok2html.rb, line 33
def files_to_str(files = [], ignore = false)
  files = files.split(",") unless defined? files.each
  files.map do |f|
    path = File.expand_path(f.strip)
    unless File.exist? path
      next if ignore
    end
    File.open(path).readlines.join + "\n" unless path.empty?
  end.join
end
footnote() click to toggle source
# File lib/mok2html.rb, line 125
def footnote
  return "" if @mok.inline_index[:footnote].nil?
  str = "<div id='mok-footnote'>"
  @mok.inline_index[:footnote].each_with_index do |f,i|
    str += %[<a id="mok-footnote-#{i+1}" class="footnote"></a>]
    str += %[<a href="#mok-footnote-#{i+1}-reverse" class="footnote-reverse">*#{i+1}</a>]
    str += " #{f[:content].map{|c| c.apply}}<br>"
  end
  str += "</div>"
  str
end
get_customized_element(file) click to toggle source

エレメントのカスタム用ファイルを読み込む

# File lib/mok2html.rb, line 50
def get_customized_element(file)
  require File.expand_path(file)
end
get_variables(str = "") click to toggle source

variables を生成する

# File lib/mok2html.rb, line 45
def get_variables(str = "")
  YAML.load( str )
end
header() click to toggle source
# File lib/mok2html.rb, line 137
    def header
      str = <<EOL
<!DOCTYPE html>
<html lang="#{@metadata[:language]}">
  <head>
    <meta charset="utf-8">
EOL
      str += css
      str += javascript
      str += "<title>#{CGI.escapeHTML(@metadata[:subject])}</title>" unless @metadata[:subject].nil?
      str += <<EOL
  </head>
<body>
EOL
    end
header_title() click to toggle source
# File lib/mok2html.rb, line 153
def header_title
  unless @metadata[:subject].nil?
    "<h1>#{CGI.escapeHTML(@metadata[:subject])}</h1>\n"
  else
    ""
  end
end
index() click to toggle source
# File lib/mok2html.rb, line 76
def index
  return "" if @mok.index[:head].nil?
  str = "<div id='mok-index'>"
  level_pre = 1
  @mok.index[:head].each_with_index do |h,i|
    next if h[:level] == 1 or h[:level] == 6

    if h[:level] == 5
      str += %[<div class="nonum"><span class="space"></span><a href="#mok-head#{h[:level]}-#{i+1}">#{CGI.escapeHTML(h[:title])}</a></div>\n]
    else
      str += index_terminate(h[:level], level_pre)
      str += "<li><a href='#mok-head#{h[:level]}-#{i+1}'>#{h[:index]}#{h[:title]}</a>\n"
      level_pre = h[:level]
    end
  end
  str += index_terminate(2, level_pre) + "</ul>"
  str += "</div>"
  str
end
index_terminate(level, level_pre) click to toggle source
# File lib/mok2html.rb, line 96
def index_terminate(level, level_pre)
  str = ""
  case level <=> level_pre
  when 1
    (level - level_pre).times do
      str += "<ul>"
    end
  when -1
    (level_pre - level).times do
      str += "</ul></li>"
    end
  else
    str += "</li>"
  end
  str
end
javascript() click to toggle source
# File lib/mok2html.rb, line 167
def javascript
  str = ""
  str += %[<script type="text/javascript">#{@javascript}</script>\n] unless @javascript.empty?
  str
end
metadata() click to toggle source
# File lib/mok2html.rb, line 113
def metadata
  str = "<div id='mok-metadata'>"
  str += %[<div>#{CGI.escapeHTML(@metadata[:description])}</div>] unless @metadata[:description].nil?
  str += %[<ul class="list-inline">]
  %w{ author create update publisher version tag }.each do |m|
    str += %[<li><strong>#{m}</strong>:#{CGI.escapeHTML(@metadata[m.to_sym])}</li>] unless @metadata[m.to_sym].nil?
  end
  str += "</ul>"
  str += "</div>"
  str
end
setup_metadata() click to toggle source
# File lib/mok2html.rb, line 54
def setup_metadata
  metadata = @mok.metadata
  metadata[:language] = @language if metadata[:language].nil?
  metadata
end
to_html() click to toggle source
# File lib/mok2html.rb, line 60
def to_html
  html = ""
  html += header unless @quiet
  html += header_title
  html += metadata if @metadata
  html += index if @index
  html += body
  html += footnote
  html += footer unless @quiet
  html
end