Class: Juli::Visitor::Html

Inherits:
Absyn::Visitor show all
Includes:
Util, Helper, TagHelper
Defined in:
lib/juli/visitor/html.rb,
lib/juli/visitor/html/helper.rb,
lib/juli/visitor/html/tag_helper.rb

Overview

This visits Absyn tree and generates HTML

Text files under juli-repository must have ‘.txt’ extention.

OPTIONS

-f

force update

-t template

specify template

Direct Known Subclasses

Slidy, TakahashiMethod

Defined Under Namespace

Modules: Helper, TagHelper Classes: HtmlLine, IdAssigner

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#header_id, #relative_from

Methods included from TagHelper

#content_tag, #tag

Methods included from Util

#camelize, conf, find_template, in_filename, juli_repo, mkdir, out_filename, str_limit, str_trim, to_wikiname, underscore, usage, visitor, visitor_list

Methods inherited from Absyn::Visitor

#visit_node

Constructor Details

#initialize(opts = {}) ⇒ Html

Html sepecific initialization does:

  1. create output_top.

  2. copy *.js, *.css files to output_top/

NOTE: this is executed every juli(1) run with -g html option (usually 99% is so), so be careful to avoid multiple initialization.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/juli/visitor/html.rb', line 110

def initialize(opts={})
  super

  # files here will not be deleted even if corresponding *.txt file
  # doesn't exist.
  @exception = {
    'sitemap' + conf['ext']       => 1,
    'recent_update' + conf['ext'] => 1,
  }

  register_helper
  register_macro
  @html_line_visitor  = HtmlLine.new(self)

  if !File.directory?(conf['output_top'])
    FileUtils.mkdir_p(conf['output_top'])
  end
  copy_to_output_top('prototype.js')
  copy_to_output_top('juli.js')
  copy_to_output_top('juli.css')
end

Instance Attribute Details

#macrosObject

Returns the value of attribute macros.



101
102
103
# File 'lib/juli/visitor/html.rb', line 101

def macros
  @macros
end

#wikiname_visitedObject

Returns the value of attribute wikiname_visited.



101
102
103
# File 'lib/juli/visitor/html.rb', line 101

def wikiname_visited
  @wikiname_visited
end

Instance Method Details

#run_bulkObject

run in bulk-mode. In Html visitor, it sync juli-repository and OUTPUT_TOP.



134
135
136
# File 'lib/juli/visitor/html.rb', line 134

def run_bulk
  sync
end

#run_file(in_file, root) ⇒ Object

visit root to generate:

1st

HTML body

2nd

whole HTML by ERB.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/juli/visitor/html.rb', line 142

def run_file(in_file, root)
  @header_sequence  = HeaderSequence.new
  IdAssigner.new.run(in_file, root)

  for key, helper in @_helpers do
    helper.on_root(in_file, root, self)
  end
  for macro_symbol, macro in @macros do
    macro.on_root(in_file, root, self)
  end

  # store to instance var for 'contents' helper
  @root       = root
  title       = File.basename(in_file.gsub(/\.[^.]*$/, ''))
  prototype   = relative_from(in_file, 'prototype.js')
  javascript  = relative_from(in_file, 'juli.js')
  stylesheet  = relative_from(in_file, 'juli.css')
  sitemap     = relative_from(in_file, 'sitemap' + conf['ext'])
  @wikiname_visited = {}
  body        = root.accept(self)

  for macro_symbol, macro in @macros do
    macro.after_root(in_file, root)
  end

  erb         = ERB.new(File.read(template))
  out_path    = out_filename(in_file, @opts[:o])
  mkdir(out_path)
  File.open(out_path, 'w') do |f|
    f.write(erb.result(binding))
  end
  printf("generated:       %s\n", out_path)
end

#template=(name) ⇒ Object



244
245
246
# File 'lib/juli/visitor/html.rb', line 244

def template=(name)
  @template = name
end

#visit_array(n) ⇒ Object



195
196
197
198
199
# File 'lib/juli/visitor/html.rb', line 195

def visit_array(n)
  n.array.inject(''){|result, child|
    result += child.accept(self)
  }
end

#visit_chapter(n) ⇒ Object



201
202
203
204
205
206
# File 'lib/juli/visitor/html.rb', line 201

def visit_chapter(n)
  header_link(n) +
  (:div, :id=>n.dom_id) do
    n.blocks.accept(self)
  end + "\n"
end

#visit_compact_dictionary_list(n) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/juli/visitor/html.rb', line 216

def visit_compact_dictionary_list(n)
  (:table, :class=>'juli_compact_dictionary') do
    n.array.inject('') do |result, child|
      result += child.accept(self)
    end
  end
end

#visit_compact_dictionary_list_item(n) ⇒ Object



224
225
226
227
228
229
# File 'lib/juli/visitor/html.rb', line 224

def visit_compact_dictionary_list_item(n)
  (:tr) do
    (:td, str2html(n.term) + ':', :nowrap=>true) +
    (:td, str2html(n.str))
  end
end

#visit_dictionary_list(n) ⇒ Object



231
232
233
234
235
236
237
# File 'lib/juli/visitor/html.rb', line 231

def visit_dictionary_list(n)
  (:dl, :class=>'juli_dictionary') do
    n.array.inject('') do |result, child|
      result += child.accept(self)
    end
  end
end

#visit_dictionary_list_item(n) ⇒ Object



239
240
241
242
# File 'lib/juli/visitor/html.rb', line 239

def visit_dictionary_list_item(n)
  (:dt, str2html(n.term), dt_css) +
  (:dd, str2html(n.str),  dd_css)
end

#visit_ordered_list(n) ⇒ Object



208
209
210
# File 'lib/juli/visitor/html.rb', line 208

def visit_ordered_list(n)
  visit_list(:ol, n)
end

#visit_str(n) ⇒ Object

if str is in list, don’t enclose by <p>



177
178
179
180
181
182
183
184
185
186
# File 'lib/juli/visitor/html.rb', line 177

def visit_str(n)
  if n.parent && n.parent.parent &&
      n.parent.parent.is_a?(Juli::Absyn::List)
    str2html(n.str)
  else
    (:p, paragraph_css) do
      str2html(n.str)
    end
  end
end

#visit_unordered_list(n) ⇒ Object



212
213
214
# File 'lib/juli/visitor/html.rb', line 212

def visit_unordered_list(n)
  visit_list(:ul, n)
end

#visit_verbatim(n) ⇒ Object



188
189
190
191
192
193
# File 'lib/juli/visitor/html.rb', line 188

def visit_verbatim(n)
  # quote; trim last white spaces at generating phase
  (:blockquote,
      (:pre, n.str.gsub(/\s+\z/m, '')),
      blockquote_css)
end