Class: Website::MarkdownConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/it_tools/html_publish.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MarkdownConverter

Returns a new instance of MarkdownConverter.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/it_tools/html_publish.rb', line 68

def initialize(options = {})
  @ops = {}
  @defaults = { :debug => false }
  unless options.nil?
    @defaults = @defaults.merge(options)
  end
  @ops = @defaults.merge(@ops)
  @debug = @ops[:debug]
  @log = Logger.new('log.txt')
  if level = @ops[:debug_level]
    @log.level = level
  else
    @log.level = Logger::DEBUG
  end
  @markdown = Redcarpet::Markdown.new(HTMLwithPygments,
                                      :fenced_code_blocks => true,
                                      :tables => true,
                                      :autolink => true,
                                      :with_toc_data => true)
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



67
68
69
# File 'lib/it_tools/html_publish.rb', line 67

def debug
  @debug
end

#logObject

Returns the value of attribute log.



67
68
69
# File 'lib/it_tools/html_publish.rb', line 67

def log
  @log
end

#markdownObject

Returns the value of attribute markdown.



67
68
69
# File 'lib/it_tools/html_publish.rb', line 67

def markdown
  @markdown
end

#opsObject

Returns the value of attribute ops.



67
68
69
# File 'lib/it_tools/html_publish.rb', line 67

def ops
  @ops
end

#write_dirObject

Returns the value of attribute write_dir.



67
68
69
# File 'lib/it_tools/html_publish.rb', line 67

def write_dir
  @write_dir
end

Instance Method Details

#convert_markdown_contents(contents) ⇒ Object



99
100
101
102
103
# File 'lib/it_tools/html_publish.rb', line 99

def convert_markdown_contents(contents)
  contents = contents.remove_non_ascii
  html = @markdown.render(contents.force_encoding('US-ASCII'))
  return html
end

#convert_markdown_file(dir, file) ⇒ Object



94
95
96
97
98
# File 'lib/it_tools/html_publish.rb', line 94

def convert_markdown_file(dir, file)
  @log.debug "Converting file: #{file}"
  contents = File.open(dir + "/" + file, "r").read
  return convert_markdown_contents(contents)
end

#copy_images_dir(read_dir, write_dirs) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/it_tools/html_publish.rb', line 238

def copy_images_dir(read_dir, write_dirs)
  src_dir = read_dir + "/images/"
  if not Dir.exists? src_dir
    @log.debug "No images directory found."
    return
  end
  @log.debug "Found images directory, will copy over now."
  write_dirs.each do |write_dir|
    files = Dir.glob(src_dir + '/*')
    files.each do |file|
      write_file = write_dir + "/" + (File.basename file).to_s
      if modded_since_last_publish(file, write_file)
        @log.debug "Copying image: " + file
        FileUtils.cp_r file, write_dir
      end
    end
  end
end

#copy_search_files(from, to) ⇒ Object



232
233
234
235
236
237
# File 'lib/it_tools/html_publish.rb', line 232

def copy_search_files(from,to) 
  files = [ "search.html", "search.js", "ajax-loader.gif", "help.png" ]
  files.each do |file|
    FileUtils.cp from + "/" + file, to
  end
end

#create_index(index, write_dir) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/it_tools/html_publish.rb', line 221

def create_index(index, write_dir)
  index.sort! { |a,b| a.downcase <=> b.downcase }
  file = File.open(write_dir + "/" +"index.html", 'w')
  file.write("<html><body><ul>")
  index.each { |filename|
    basename = File.basename(filename, File.extname(filename))
    file.write("<li/><a href=\"#{basename}.html\">#{basename}")
  }
  file.write("</ul></body></html>")
  file.close()
end

#get_file_basename(file) ⇒ Object



110
111
112
# File 'lib/it_tools/html_publish.rb', line 110

def get_file_basename(file)
  basename = File.basename(file, File.extname(file))
end

#get_files_to_process(doc_dir, write_dir) ⇒ Object



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
# File 'lib/it_tools/html_publish.rb', line 147

def get_files_to_process(doc_dir, write_dir)
  @log.debug "Begin processing directory: " + doc_dir
  Dir.chdir(doc_dir)
  all_mmd_files = []
  files_to_process = []
  Dir.foreach(doc_dir) do |file|
    $last_header_level = 0
    $last_header_count = 0
    $header = Array.new
    0.upto(6) { |i| $header[i] = 0 }
    convertedFile = false
    if file == ".." or file == "."
      @log.debug "Current entry is: #{file}, do nothing."
      next;
    end
    if file.match(/^\.#/)
      @log.debug "Do not process files that begin with .#.  Filename: #{file}"
      next;
    end
    if File.stat(doc_dir + "/" + file).directory?
      @log.debug "#{file} is a directory, skip it."
      next;
    end
    if File.extname(file) == ".mmd"
      @log.debug "Found MultiMarkdown file: #{file}"
      if modified_since_last_publish(doc_dir, file, write_dir)
        files_to_process.push file
      end
      all_mmd_files.push(file);
      next
    end
    @log.debug "File: #{file} is not a known format, skipping."
  end
  return files_to_process, all_mmd_files
end

#get_out_file(file, write_dir) ⇒ Object



104
105
106
107
108
109
# File 'lib/it_tools/html_publish.rb', line 104

def get_out_file(file, write_dir)
  basename = File.basename(file, File.extname(file))
  @log.debug "Filename without extension: #{basename}"
  out_file = write_dir + "/" + basename + ".html"
  return out_file
end

#goObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/it_tools/html_publish.rb', line 257

def go()
  before = Time.now

  read_dir = "/home/fenton/projects/documentation"
  write_dir = "/home/fenton/bin/website/"
  write_dir_static = "/home/fenton/bin/website_static"

  files_to_process, all_mmd_files = get_files_to_process read_dir, write_dir
  create_index all_mmd_files, write_dir_static
  process_files files_to_process, read_dir, :for_indexer_dir => write_dir, :for_static_files_dir => write_dir_static
  copy_images_dir read_dir, [write_dir,write_dir_static]
  copy_search_files read_dir, write_dir
  write_stylesheet read_dir, write_dir
  write_stylesheet read_dir, write_dir_static

  read_dir = "/home/fenton/projects/work-doco/docs"
  write_dir = "/home/fenton/bin/work-doco/"
  write_dir_static = "/home/fenton/bin/work-doco_static/"

  files_to_process, all_mmd_files = get_files_to_process read_dir, write_dir
  write_dirs = {
    :for_indexer_dir => write_dir, 
    :for_static_files_dir => write_dir_static }
  process_files files_to_process, read_dir, write_dirs
  copy_images_dir read_dir, [write_dir,write_dir_static]
  write_stylesheet read_dir, write_dir
  write_stylesheet read_dir, write_dir_static
  @log.debug "Elapsed time: #{Time.now - before}(s)"
end

#modded_since_last_publish(src_file, write_file) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/it_tools/html_publish.rb', line 120

def modded_since_last_publish(src_file, write_file)
  src_file_ts = File.stat(src_file).mtime
  if not File.exist?(write_file)
    @log.debug "Target file doesn't exist, will copy source to dest: #{write_file}"
    return true
  end
  write_file_ts = File.stat(write_file).mtime
  @log.debug "[SRC]:#{src_file} - #{src_file_ts}"
  @log.debug "[DEST]:#{write_file} - #{write_file_ts}"

  # should return true or false depending on modification times...
  if write_file_ts > src_file_ts
    @log.debug "Source file older than output file, no need to regenerate."
    return false
  else
    @log.debug "Source file more recent than output file, need to regenerate."
    return true
  end
end

#modified_since_last_publish(read_dir, file, write_dir) ⇒ Object



140
141
142
143
144
145
# File 'lib/it_tools/html_publish.rb', line 140

def modified_since_last_publish(read_dir, file, write_dir)
  basename = File.basename(file, File.extname(file))
  write_file = write_dir + "/" + basename + ".html"
  src_file = read_dir + "/" + file
  return modded_since_last_publish src_file, write_file
end

#process_file(file, read_dir, write_dirs) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/it_tools/html_publish.rb', line 193

def process_file(file, read_dir, write_dirs) 
  @log.debug "Found file: " 
  @log.debug read_dir.inspect
  @log.debug file.inspect
  indexer_dir = write_dirs[:for_indexer_dir]
  static_site_dir = write_dirs[:for_static_files_dir]
  indexer_file_path = get_out_file(file, indexer_dir)
  static_file_path = get_out_file(file, static_site_dir)
  html = convert_markdown_file(read_dir, file) 
  write_contents_no_style(indexer_file_path, html)
  write_contents_add_style(static_file_path,html)
  send_to_indexer(indexer_file_path, html)
end

#process_files(files_to_process, read_from_dir, write_dirs) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/it_tools/html_publish.rb', line 182

def process_files(files_to_process, read_from_dir, write_dirs)
  @log.debug write_dirs.inspect
  @log.debug "files to process are:"
  @log.debug files_to_process.inspect
  @log.debug "number of files to process is: " + files_to_process.length.to_s
  files_to_process.each do |file|
    @log.debug "Current file is:"
    @log.debug file.inspect
    process_file file, read_from_dir, write_dirs
  end
end

#send_to_indexer(file_path, html) ⇒ Object



206
207
208
209
210
# File 'lib/it_tools/html_publish.rb', line 206

def send_to_indexer(file_path, html)
  uploader = Solr::Upload.new(:solr_base_url => ops[:indexer_url])
  file_name = get_file_basename(file_path)
  uploader.upload_file( file_name, html, file_name )
end

#set_options(options = {}) ⇒ Object



88
89
90
# File 'lib/it_tools/html_publish.rb', line 88

def set_options(options = {})
  @ops = @ops.merge(options)
end

#to_markdown(content) ⇒ Object



91
92
93
# File 'lib/it_tools/html_publish.rb', line 91

def to_markdown(content)
  return markdown.render(content)
end

#write_contents_add_style(out_file, html) ⇒ Object



215
216
217
218
219
220
# File 'lib/it_tools/html_publish.rb', line 215

def write_contents_add_style(out_file, html)
  @log.debug "Writing file: #{out_file}"
  style_info = '<head><LINK REL=StyleSheet HREF="style.css" TYPE="text/css"/></head>'
  @log.debug "Writing html + style info to file: " + out_file
  File.open(out_file, 'w') {|f| f.write(style_info + html) }
end

#write_contents_no_style(out_file, html) ⇒ Object



211
212
213
214
# File 'lib/it_tools/html_publish.rb', line 211

def write_contents_no_style(out_file, html)
  @log.debug "Writing file: #{out_file}"
  File.open(out_file, 'w') {|f| f.write(html) }
end

#write_stylesheet(read_dir, write_dir) ⇒ Object



114
115
116
117
118
# File 'lib/it_tools/html_publish.rb', line 114

def write_stylesheet(read_dir,write_dir)
  contents = File.open(read_dir + "/inputStyles.css", "rb").read
  css = Pygments.css
  File.open(write_dir + "/style.css", 'w') {|f| f.write(css + "\n" + contents) }
end