Module: Soywiki::Html

Defined in:
lib/soywiki/html.rb

Constant Summary collapse

PAGE_TEMPLATE =
File.read(File.join(File.dirname(__FILE__), '..', 'page_template.html.haml'))
HTML_DIR =
'soywiki-html-export'
INDEX_PAGE_TEMPLATE =
File.read(File.join(File.dirname(__FILE__), '..', 'index_template.html.haml'))

Class Method Summary collapse

Class Method Details

.exportObject



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

def self.export
  `rm -rf #{HTML_DIR}/*`
  namespaces = Dir["*"].select {|f| 
    File.directory?(f) && f != HTML_DIR
  }.sort.map {|namespace|
    count = Dir["#{namespace}/*"].select {|f| wiki_page?(f)}.size
    [namespace, count]
  }
  namespaces.each do |namespace_dir, count|
    make_pages namespace_dir, namespaces
  end
  # make root index page
  make_root_index_page namespaces
  puts "HTML files written to #{HTML_DIR}/"
end

.generate_page(text, namespace, pages, namespaces) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/soywiki/html.rb', line 30

def self.generate_page(text, namespace, pages, namespaces)
  title = text.split("\n")[0]
  body = process(text.split("\n")[1..-1].join("\n").strip)
  Haml::Engine.new(PAGE_TEMPLATE).render(nil, :body => body, 
                                         :title => title, 
                                         :namespace => namespace,
                                         :namespaces => namespaces, :pages => pages)
end


17
18
19
20
21
22
# File 'lib/soywiki/html.rb', line 17

def self.href_hyperlinks(text)
  text = text.gsub(HYPERLINK) {|match|
    %{<a href="#{match}">#{match}</a>}
  }
  return text
end


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/soywiki/html.rb', line 5

def self.href_wiki_links(text)
  text = text.gsub(WIKI_WORD) {|match|
    href = if match =~ /\w\./ # namespace
      "../#{match.gsub(".", "/")}.html"
    else
      match + '.html'
    end
    %{<a href="#{href}">#{match}</a>}
  }
  return text
end

.make_index_page(dir, pages, namespaces) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/soywiki/html.rb', line 47

def self.make_index_page(dir, pages, namespaces)
  outfile = File.join(HTML_DIR, dir, 'index.html')
  html = Haml::Engine.new(INDEX_PAGE_TEMPLATE).render(nil, 
                                         :namespace => dir, 
                                         :root => false,
                                         :pages => pages.map {|p| p.split('/')[1]}.sort, 
                                         :namespaces => namespaces)
  File.open(outfile, 'w') {|f| f.write html}
  # puts "=> Writing #{outfile}"
end

.make_pages(dir, namespaces) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/soywiki/html.rb', line 58

def self.make_pages(dir, namespaces)
  `mkdir -p #{HTML_DIR}/#{dir}`
  pages = Dir["#{dir}/*"].select {|file| wiki_page? file} 
  # make pages
  pages.each do |file|
    outfile =  File.join(HTML_DIR, file + '.html')
    html = Soywiki::Html.generate_page(File.read(file), 
                                       dir, 
                                       pages.map {|p| p.split('/')[1]}.sort, 
                                       namespaces)
    File.open(outfile, 'w') {|f| f.write html}
    # puts "Writing #{outfile}"
  end
  make_index_page(dir, pages, namespaces)
end

.make_root_index_page(namespaces) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/soywiki/html.rb', line 74

def self.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}
  # puts "=> Writing #{outfile}"
end

.process(t) ⇒ Object



24
25
26
# File 'lib/soywiki/html.rb', line 24

def self.process(t)
  href_hyperlinks(href_wiki_links(t))
end

.wiki_page?(file) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/soywiki/html.rb', line 43

def self.wiki_page?(file)
  file.gsub("/", '.') =~ WIKI_WORD
end