Sha256: cbafb9362aa2c5d00b3805bd5f29038debe413dddf194b0217441b3271e0d15f

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

#!/usr/bin/env ruby

ID_REGEX = / #([a-z_A-Z\-]+)/
CLASS_REGEX = / \.([a-z_A-Z\-]+)/

def parse_text(text)
	output = ""
	tokenizer = HTML::Tokenizer.new(text)
	while token = tokenizer.next
		if token[0] == "<" and token[token.length - 1] == ">" and token[1] != "/"
			#if it's an opening tag, analyze it

			#find and replace fira ID attribute
			result = token.sub(ID_REGEX, ' id="\1"')

			#find fira class attributes
			classes = result.scan(CLASS_REGEX)

			if ! classes.empty?
			
				#build an HTML class attribute
				att = 'class="'
			
				classes.each do |cl|
					att += " #{cl[0]}"
				end
				
				att += '"'
				
				#remove the space before the first class
				att = att.sub(/class=" /, ' class="')

				#remove the first fira class attribute
				new_tag = result.sub(CLASS_REGEX, att)
				
				#remove the rest of the fira class attributes
				final_result = new_tag.gsub(CLASS_REGEX, "")

				output += final_result
			else
				output += result
			end

		else
			output += token
		end
	end

	return output
end



#####################
# START HERE
#####################

files = []
ARGV.each do |a|
	files.push a
end

files.each do |fi|
	File.open(fi, 'r+'){
		|f|
		contents = f.read
		
		result = parse_text(contents)
		
		#make the new file's name
		new_file = fi.sub(/(\.fira$|\.html.fira$)/, ".html")
		
		File.open(new_file, "w") { |nf| nf.write(result)}
	}
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fira-0.6.2 bin/fira.rb
fira-0.6.1 bin/fira.rb
fira-0.6.0 bin/fira.rb
fira-0.5.3 bin/fira.rb
fira-0.5.2 bin/fira.rb
fira-0.5.1 bin/fira.rb