Sha256: 2403532dea6ab952c0f7055cfb652c92338752f9b620ce027bd67de1ad6d9547

Contents?: true

Size: 1.39 KB

Versions: 12

Compression:

Stored size: 1.39 KB

Contents

require File.expand_path(File.dirname(__FILE__) + "/../lib/mizuho")
require 'digest/md5'
require 'mizuho/generator'

CACHE_DIR = "#{Mizuho::SOURCE_ROOT}/test/cache"

def generate_and_parse(text)
	Dir.mkdir(CACHE_DIR) if !File.exist?(CACHE_DIR)
	
	# Unindent text.
	lines = text.split(/\r?\n/)
	min_indenting = nil
	lines.each do |line|
		next if line.strip.empty?
		line =~ /\A([\t\s]*)/
		if min_indenting.nil? || $1.size < min_indenting
			min_indenting = $1.size
		end
	end
	if min_indenting
		lines.map! do |line|
			line[min_indenting..-1]
		end
	end
	text = lines.join("\n")
	
	output_filename = File.join(CACHE_DIR, Digest::MD5.hexdigest(text)) + ".html"
	
	# Generate Asciidoc output if it isn't cached, otherwise use cached version.
	# Also check whether the cache is newer than Asciidoc; we want to invalidate
	# the cache upon upgrading Asciidoc.
	if !File.exist?(output_filename) || asciidoc_newer_than?(output_filename)
		input_filename = File.join(CACHE_DIR, "input.#{Process.pid}.txt")
		begin
			File.open(input_filename, 'w') do |f|
				f.write(text)
			end
			Mizuho::Generator.run_asciidoc(input_filename, output_filename)
		ensure
			File.unlink(input_filename) rescue nil
		end
	end
	
	return Mizuho::Parser.new(output_filename)
end

def asciidoc_newer_than?(filename)
	asciidoc_mtime = File.stat("#{Mizuho::SOURCE_ROOT}/asciidoc/asciidoc.py").mtime
	return asciidoc_mtime > File.stat(filename).mtime
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mizuho-0.9.20 test/spec_helper.rb
mizuho-0.9.19 test/spec_helper.rb
mizuho-0.9.18 test/spec_helper.rb
mizuho-0.9.17 test/spec_helper.rb
mizuho-0.9.15 test/spec_helper.rb
mizuho-0.9.14 test/spec_helper.rb
mizuho-0.9.13 test/spec_helper.rb
mizuho-0.9.12 test/spec_helper.rb
mizuho-0.9.11 test/spec_helper.rb
mizuho-0.9.10 test/spec_helper.rb
mizuho-0.9.9 test/spec_helper.rb
mizuho-0.9.8 test/spec_helper.rb