Sha256: cd018f37f90a3b3de0f341e24761156221162da2f9fdfa5ea479d19436a02c5e

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

#!/usr/bin/env ruby

ID_PATTERN = /(<.*)#([a-z_A-Z\-]+)(.*>.*<\/.*>)/
CLASS_PATTERN = /\.([a-z_A-Z\-]+)/
TAG_PATTERN = /<.*>/
OPEN_TAG_PATTERN = /(<.*>)(.*<\/.*>)/

#begin parsing text
def parse_text(contents)
	
	#find id's
	result = parse_ids(contents)
	
	#find classes
	output = parse_classes(result)
		
end

#scan for ids and replace with html id attributes
def parse_ids(contents)

	result = contents.gsub(ID_PATTERN, '\1 id="\2" \3')
end

#scan for classes and create html class attributes
def parse_classes(contents)

	result = contents
	
	#scan for classes
	tags = contents.scan(TAG_PATTERN)
	tags.each do |tag|

		open_tag = tag.scan(OPEN_TAG_PATTERN)

		classes = open_tag[0][0].scan(CLASS_PATTERN)

		if ! classes.empty?
		
			#build a 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 fira class attributes
			new_open_tag = open_tag[0][0].gsub(CLASS_PATTERN, "")

			#save the html class attributes back into the tag
			new_open_tag = new_open_tag.sub(/>/,att + "\\0")
			
			#replace the old opening tag with the new
			new_tag = tag.gsub(OPEN_TAG_PATTERN, new_open_tag + '\2')
			
			#save the whole html tag back into the file
			result = result.sub(tag, new_tag)
		end
	end
	
	return result
end


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

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

files.each do |fi|
	File.open(fi, 'r+'){
		|f|
		contents = f.read

		tag_pattern = /<.*>/
		
		result = parse_ids(contents)
		
		result = parse_classes(result, tag_pattern)
		
		#make the new file's name
		new_file = fi.sub(/(\.fi$|\.html.fi$)/, ".html")
		
		File.open(new_file, "w") { |nf| nf.write(result)}
	}
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fira-0.3.0 bin/fira.rb
fira-0.2.0 bin/fira.rb