Class: Soywiki::Html

Inherits:
Object
  • Object
show all
Includes:
Template_Substitution
Defined in:
lib/soywiki/html.rb

Constant Summary collapse

HTML_DIR =
'html-export'
INDEX_PAGE_TEMPLATE =
File.read(File.join(File.dirname(__FILE__), '..', 'index_template.html.haml'))
%r|\[([^\]]+)\]\(\[(#{HYPERLINK})\]\(\2\)\)|
PAGE_TEMPLATE =
File.read(File.join(File.dirname(__FILE__), '..', 'page_template.html.haml'))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown, relative_soyfile) ⇒ Html

Returns a new instance of Html.



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

def initialize(markdown, relative_soyfile)
  @markdown = markdown
  @relative_soyfile = relative_soyfile
end

Instance Attribute Details

#current_namespaceObject (readonly)

Returns the value of attribute current_namespace.



12
13
14
# File 'lib/soywiki/html.rb', line 12

def current_namespace
  @current_namespace
end

#markdownObject (readonly)

Returns the value of attribute markdown.



12
13
14
# File 'lib/soywiki/html.rb', line 12

def markdown
  @markdown
end

#relative_soyfileObject (readonly)

Returns the value of attribute relative_soyfile.



12
13
14
# File 'lib/soywiki/html.rb', line 12

def relative_soyfile
  @relative_soyfile
end

Class Method Details

.export(markdown, relative_soyfile) ⇒ Object



14
15
16
# File 'lib/soywiki/html.rb', line 14

def self.export(markdown, relative_soyfile)
  new(markdown, relative_soyfile).export
end

Instance Method Details

#absolute_soyfile_path(path) ⇒ Object



143
144
145
146
147
148
# 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) ⇒ Object



150
151
152
# File 'lib/soywiki/html.rb', line 150

def absolutify(path)
  File.absolute_path(File.join(wiki_root, path))
end

#choose_soyfile_path(path) ⇒ Object



133
134
135
136
137
138
139
140
141
# 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_dirObject



34
35
36
# File 'lib/soywiki/html.rb', line 34

def clear_html_dir
  `rm -rf #{HTML_DIR}/*`
end

#exportObject



23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/soywiki/html.rb', line 103

def href_hyperlinks(text)
  substitute =
    if markdown
      '[\\0](\\0)'
    else
      '<a href="\\0">\\0</a>'
    end
  text = text.gsub(HYPERLINK, substitute)
  if markdown
    text = text.gsub(BROKEN_MARKDOWN_HYPERLINK, '[\\1](\\2)')
  end
  text.gsub(HYPERLINK) { |uri| soyfile_to_href(uri) }
end


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/soywiki/html.rb', line 91

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

#index_page_templateObject



180
181
182
183
184
185
186
# 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) ⇒ Object



170
171
172
173
174
175
176
177
178
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



188
189
190
191
192
193
194
195
196
# 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) ⇒ Object



158
159
160
# File 'lib/soywiki/html.rb', line 158

def markdownify(text)
  RDiscount.new(text).to_html.gsub("<pre><code>", "<pre><code>\n")
end

#namespacesObject



38
39
40
41
42
43
44
45
# 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_templateObject



162
163
164
165
166
167
168
# 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) ⇒ Object



87
88
89
# File 'lib/soywiki/html.rb', line 87

def process(text)
  href_hyperlinks(href_wiki_links(text))
end

#soyfile_match(uri) ⇒ Object



127
128
129
130
131
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
# 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) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/soywiki/html.rb', line 47

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

#wiki_pages(namespace) ⇒ Object



63
64
65
# File 'lib/soywiki/html.rb', line 63

def wiki_pages(namespace)
  Dir["#{namespace}/*"].select { |file| wiki_page?(file) }
end

#wiki_rootObject



154
155
156
# File 'lib/soywiki/html.rb', line 154

def wiki_root
  Dir.getwd
end