class Soywiki::Html

Constants

HTML_DIR
INDEX_PAGE_TEMPLATE
PAGE_TEMPLATE

Attributes

current_namespace[R]
markdown[R]
relative_soyfile[R]

Public Class Methods

export(markdown, relative_soyfile) click to toggle source
# File lib/soywiki/html.rb, line 14
def self.export(markdown, relative_soyfile)
  new(markdown, relative_soyfile).export
end
new(markdown, relative_soyfile) click to toggle source
# File lib/soywiki/html.rb, line 18
def initialize(markdown, relative_soyfile)
  @markdown = markdown
  @relative_soyfile = relative_soyfile
end

Public Instance Methods

absolute_soyfile_path(path) click to toggle source
# File lib/soywiki/html.rb, line 143
def absolute_soyfile_path(path)
  return path if path[0] == '/'
  autochdir_path = absolutify("#{current_namespace}/#{path}")
  wiki_path = absolutify(path)
  File.exists?(autochdir_path) ? autochdir_path : wiki_path
end
absolutify(path) click to toggle source
# File lib/soywiki/html.rb, line 150
def absolutify(path)
  File.absolute_path(File.join(wiki_root, path))
end
choose_soyfile_path(path) click to toggle source
# File lib/soywiki/html.rb, line 133
def choose_soyfile_path(path)
  absolute_path = absolute_soyfile_path(path)
  if relative_soyfile
    Pathname.new(absolute_path).
      relative_path_from(Pathname.new(wiki_root)).to_s
  else
    absolute_path
  end
end
clear_html_dir() click to toggle source
# File lib/soywiki/html.rb, line 34
def clear_html_dir
  `rm -rf #{HTML_DIR}/*`
end
export() click to toggle source
# File lib/soywiki/html.rb, line 23
def export
  clear_html_dir
  @current_namespace = nil
  namespaces.each do |namespace, _count|
    make_pages(namespace)
  end
  make_root_index_page(namespaces)
  @current_namespace = nil
  puts "HTML files written to #{HTML_DIR}/"
end
generate_page(text, namespace, pages) click to toggle source
# File lib/soywiki/html.rb, line 67
def generate_page(text, namespace, pages)
  @current_namespace = namespace
  text = text.split("\n")

  title = text.shift || ''
  body = ''
  unless text.empty?
    body = process(text.join("\n").strip)
    body = markdownify(body) if markdown
  end

  Haml::Engine.new(page_template).
    render(nil, :body => body,
           :title => title,
           :namespace => namespace,
           :namespaces => namespaces,
           :pages => pages,
           :markdown => markdown)
end
index_page_template() click to toggle source
# File lib/soywiki/html.rb, line 180
def index_page_template
  if defined?(INDEX_PAGE_TEMPLATE_SUB)
    INDEX_PAGE_TEMPLATE_SUB
  else
    INDEX_PAGE_TEMPLATE
  end
end
make_index_page(namespace, inner_pages) click to toggle source
# File lib/soywiki/html.rb, line 170
def make_index_page(namespace, inner_pages)
  outfile = File.join(HTML_DIR, namespace, 'index.html')
  html = Haml::Engine.new(index_page_template).
    render(nil, :namespace => namespace,
           :root => false,
           :pages => inner_pages,
           :namespaces => namespaces)
  File.open(outfile, 'w') { |f| f.write(html) }
end
make_pages(namespace) click to toggle source
# File lib/soywiki/html.rb, line 51
def make_pages(namespace)
  `mkdir -p #{HTML_DIR}/#{namespace}`
  pages = wiki_pages(namespace)
  inner_pages = pages.map { |p| p.split('/')[1] }.sort
  pages.each do |file|
    outfile =  File.join(HTML_DIR, file + '.html')
    html = generate_page(File.read(file), namespace, inner_pages)
    File.open(outfile, 'w') { |f| f.write(html) }
  end
  make_index_page(namespace, inner_pages)
end
make_root_index_page(namespaces) click to toggle source
# File lib/soywiki/html.rb, line 188
def make_root_index_page(namespaces)
  outfile = File.join(HTML_DIR, 'index.html')
  html = Haml::Engine.new(index_page_template).
    render(nil, :namespace => nil,
           :pages => [],
           :root => true,
           :namespaces => namespaces)
  File.open(outfile, 'w') { |f| f.write(html) }
end
markdownify(text) click to toggle source
# File lib/soywiki/html.rb, line 158
def markdownify(text)
  RDiscount.new(text).to_html.gsub("<pre><code>", "<pre><code>\n")
end
namespaces() click to toggle source
# File lib/soywiki/html.rb, line 38
def namespaces
  @namespaces ||= Dir["*"].select do |file|
    File.directory?(file) && file != HTML_DIR
  end.sort.map do |namespace|
    count = Dir["#{namespace}/*"].select { |f| wiki_page?(f) }.size
    [namespace, count]
  end
end
page_template() click to toggle source
# File lib/soywiki/html.rb, line 162
def page_template
  if defined?(PAGE_TEMPLATE_SUB)
    PAGE_TEMPLATE_SUB
  else
    PAGE_TEMPLATE
  end
end
process(text) click to toggle source
# File lib/soywiki/html.rb, line 87
def process(text)
  href_hyperlinks(href_wiki_links(text))
end
soyfile_match(uri) click to toggle source
# File lib/soywiki/html.rb, line 127
def soyfile_match(uri)
  uri_after_scheme = %r{[^ >)\n\]]+}
  regex = %r{^soyfile://(#{uri_after_scheme})}
  uri.match(regex)
end
soyfile_to_href(uri) click to toggle source
# File lib/soywiki/html.rb, line 117
def soyfile_to_href(uri)
  match = soyfile_match(uri)
  if match
    path = choose_soyfile_path(match[1])
    path[0] == '/' ? "file://#{path}" : path
  else
    uri
  end
end
wiki_page?(file) click to toggle source
# File lib/soywiki/html.rb, line 47
def wiki_page?(file)
  file.gsub("/", '.') =~ WIKI_WORD
end
wiki_pages(namespace) click to toggle source
# File lib/soywiki/html.rb, line 63
def wiki_pages(namespace)
  Dir["#{namespace}/*"].select { |file| wiki_page?(file) }
end
wiki_root() click to toggle source
# File lib/soywiki/html.rb, line 154
def wiki_root
  Dir.getwd
end