Method: Bookmaker::Base.generate_html

Defined in:
lib/bookmaker/base.rb

.generate_htmlObject



131
132
133
134
135
136
137
138
139
140
141
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/bookmaker/base.rb', line 131

def self.generate_html
  # all parsed markdown file holder
  contents = ""
  
  # first, get all chapters; then, get all parsed markdown
  # files from this chapter and group them into a <div class="chapter"> tag
  Dir.entries(text_dir).sort.each do |dirname|
    # ignore files and some directories
    next if %w(. .. .svn .git).include?(dirname) || File.file?(text_dir + "/#{dirname}")
    
    # gets all parsed markdown files to wrap in a 
    # chapter element
    chapter = ""
    
    # merge all markdown and textile files into a single list
    markup_files = Dir["#{text_dir}/#{dirname}/**/*.markdown"] + Dir["#{text_dir}/#{dirname}/**/*.textile"]
    
    # no files, so skip it!
    next if markup_files.empty?
    
    markup_files.sort.each do |markup_file|
      # get the file contents
      markup_contents = File.new(markup_file).read
      
      # instantiate a markup object
      begin
        if markup_file =~ /\.textile$/
          markup = BlackCloth.new(markup_contents)
        else
          markup = Discount.new(markup_contents)
        end
      rescue Exception => e
        puts "Skipping #{markup_file} (#{e.message})"
        next
      end
      
      # convert the markup into html
      parsed_contents = markup.to_html
      
      if Object.const_defined?('Uv')
        if markup.respond_to?(:syntax_blocks)
          # textile
          parsed_contents.gsub!(/@syntax:([0-9]+)/m) do |m|
            syntax, code = markup.syntax_blocks[$1.to_i]
            Bookmaker::Markup.syntax(code, syntax)
          end
        else
          # markdown
          parsed_contents.gsub! /<pre><code>(.*?)<\/code><\/pre>/m do |block|
            code = $1.gsub(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&amp;/, '&')
            code_lines = StringIO.new(code).readlines
            syntax_settings = code_lines.first
            
            syntax = 'plain_text'
            
            if syntax_settings =~ /syntax\(.*?\)\./
              code = code_lines.slice(1, code_lines.size).join
              
              # syntax
              m, syntax = *syntax_settings.match(/syntax\(([^ #]+).*?\)./)
              
              # file name
              m, source_file = *syntax_settings.match(/syntax\(.*?\)\. +(.*?)$/)
              
              # get line interval
              m, from_line, to_line = *syntax_settings.match(/syntax\(.*? ([0-9]+),([0-9]+)\)/)
              
              # get block name
              m, block_name = *syntax_settings.match(/syntax\(.*?#([0-9a-z_]+)\)/)
              
              code = Bookmaker::Markup.content_for({
                :code => code,
                :from_line => from_line,
                :to_line => to_line,
                :block_name => block_name,
                :source_file => source_file
              })
              
              Bookmaker::Markup.syntax(code, syntax)
            end
          end
        end
      end
      
      chapter << (parsed_contents + "\n\n")
    end
    
    contents << '<div class="chapter">%s</div>' % chapter
  end

  # save html file
  File.open(html_path, 'w+') do |f|
    f << Bookmaker::Base.parse_layout(contents)
  end
end