Skip to content

Instantly share code, notes, and snippets.

@michaelfward
Last active August 29, 2015 14:17
Show Gist options
  • Save michaelfward/9c14224541f2076a70d3 to your computer and use it in GitHub Desktop.
Save michaelfward/9c14224541f2076a70d3 to your computer and use it in GitHub Desktop.
If you're like me, you write a ton of HTML+CSS when doing web development, and write all the javascript later. Point this script at your html files to automatically generate function declarations. NOTE: move the script file after use, because it will be destroyed each time
#coded by michael ward
#i wrote a bunch of client facing code to render different stuffs
#and decided to write all the javascript functions later and
#only focus on the CSS and HTML, and i realized it was a pain
#to go and get all the functions i needed to write, etc... so..
#voila
#*-------------------------------------------------*
#i want to port it to be able to take 1/2 argument *
#that would either a) read dirs for all html files *
#and then build all the js functions, and b) take *
#an argument that specifies which language to build*
#the functions in OR which language to extract from*
#SO if you like that idea, stay tuned or fork *
#*-------------------------------------------------*
#[email protected]
unless ARGV.length == 1
puts "must supply file as argument"
exit
end
fd = File.open(ARGV[0], "r")
out = File.open("./script.js", "w")
reg = /.*onclick=['"]([a-zA-Z_]*).*/
regg = /.*(\([a-zA-Z"]*\)).*/
$arr = []
def check(str)
$arr.each do |line|
if str.chomp == line.chomp
return false
end
end
true
end
fd.each_line do |line|
unless (reg =~ line) != 0
a = Regexp.last_match
unless (regg =~ line ) != 0
d = Regexp.last_match
str = "function #{a[1]}#{d[1]}{\n}\n"
if d[1].length == 2
out.write(str)
$arr.push(str)
else
out.write("function #{a[1]}(arg){\n}\n)") unless check(str) == false
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment