Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
indentxml

indentxml

Ratings:

4.0

(1)
|Views: 43|Likes:
Published by matthieutc
ruby script to indent xml/xhtml files
ruby script to indent xml/xhtml files

More info:

Published by: matthieutc on Sep 30, 2007
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

06/16/2009

pdf

text

original

 
#!/usr/bin/env ruby#written by mats 2007require 'optparse'class MyOptions < Hashattr_accessor :filesdef initialize(args)super()@files = []self[:indent] = 4self[:html_mode] = trueself[:streaming] = falseself[:in_place] = falseopts = OptionParser.new do |opts|opts.banner = <<EOFUsage: #$0 [options] filenameThis script indents XML files. It is web template friendly, as it does not touchanything that is not valid XML.EOFopts.on('-i', '--in-place', 'overwrites the file in place') doself[:in_place] = trueendopts.on('-s', '--streaming', 'accepts input from stdin, not files') doself[:streaming] = trueendopts.on('-a', '--alltags', 'indent all xml elements (not just html)') doself[:html_mode] = falseendopts.on('-n N', '--indent=N', 'number of spaces to use as one indent level')do |string|self[:indent] = string.to_i()endopts.on_tail('-h', '--help', 'display this help and exit') doputs optsexitendendopts.order!(args) {|opt| @files << opt}endendarguments = MyOptions.new(ARGV)@tag = /(<[^\/].+?>)|(<\/.+?>)/@indent_step = arguments[:indent]@html_mode = arguments[:html_mode]@streaming = arguments[:streaming]@in_place = arguments[:in_place]@initial_indent = ''@first_tag_encountered = false@inlines = ['</a>','</h1>','</h2>','</h3>','</h4>','</h5>','</h6>','</textarea>']@html_elements =['A',
 
'ABBR','ACRONYM','ADDRESS','APPLET','AREA','B','BASE','BASEFONT','BDO','BIG','BLOCKQUOTE','BODY','BR','BUTTON','CAPTION','CENTER','CITE','CODE','COL','COLGROUP','DD','DEL','DFN','DIR','DIV','DL','DT','EM','FIELDSET','FONT','FORM','FRAME','FRAMESET','H1','H2','H3','H4','H5','H6','HEAD','HR','HTML','I','IFRAME','IMG','INPUT','INS','ISINDEX','KBD','LABEL','LEGEND','LI','LINK','MAP','MENU','META','NOFRAMES','NOSCRIPT',
 
'OBJECT','OL','OPTGROUP','OPTION','P','PARAM','PRE','Q','S','SAMP','SCRIPT','SELECT','SMALL','SPAN','STRIKE','STRONG','STYLE','SUB','SUP','TABLE','TBODY','TD','TEXTAREA','TFOOT','TH','THEAD','TITLE','TR','TT','U','UL','VAR']#a little argument checking to help Joeif @streaming and @in_placeputs "Options 'streaming' and 'in place' do not make sense together.\n\If you want to indent a streaming input, do not specify '-i'\n\Exiting..."exitelsif !@streaming and arguments.files.empty?puts 'give me at least one file to indent!'exitenddef indent(file)stack = []results = ''cur_indent = 0line_no = 0file.each { |line|line_no += 1#puts "line: #{line}"md = @tag.match linesame_line = falsewhile mdpre = md.pre_matchtag_name = md[0][1...-1].split()[0].upcase! #the first word of the text in

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->