for all the germans using textmate for html-coding this little command will lookup the current html-element on selfhtml.org. i'm sorry as of now there's no english version of this excellent website available.
regards, niko.
#!/usr/bin/env ruby # # open element reference on de.selfhtml.org # this is certainly a BAD implementation # works for me, though # any improvements are highly appreciated # niko.
require 'net/http' $tag_name = ENV["TM_CURRENT_WORD"].to_s
# get the elemnte.htm and attribute.htm: Net::HTTP.start( 'de.selfhtml.org', 80 ) do |http| $html_elemente = http.get( "/html/referenz/elemente.htm" ).body $html_attribute = http.get( "/html/referenz/attribute.htm" ).body end
# strip the head and the current tag: $html_elemente.gsub!(/(<!.*<body>).*(<h2><a class="an" name="# {$tag_name}">#{$tag_name}</a></h2>.*?)<h2>.*/m,'\1\2')
# prepend "elemente.htm" to anchor-links: $html_elemente.gsub!(/href="#(.*?")/,'href="elemente.htm#\1')
# the current tag: $html_attribute.gsub!(/.*(<h2><a class="an" name="#{$tag_name}"># {$tag_name}</a></h2>.*?)<h2>.*/m,'\1')
# prepend "elemente.htm" to anchor-links: $html_attribute.gsub!(/href="#(.*?")/,'href="attribute.htm#\1')
# concat the two strings: $html = $html_elemente + $html_attribute
# turn relative links into absolute links (for hrefs and imgs): $html.gsub!(/href="(.*?")/,'href="http://de.selfhtml.org/html/ referenz/\1') $html.gsub!(/src="(.*?")/,'src="http://de.selfhtml.org/html/referenz/ \1')
# output the html puts $html
i just realized that there's s/th wrong with the char-encoding with this command. can't figure out what it is. :(
n.
Hey Niko,
very nice. You might have a look here: http://www.ruby-doc.org/stdlib/libdoc/rss/ rdoc/classes/RSS/Converter.html to convert the iso-8859-1 to utf-8 maybe.
I could not get it to work though :)
Stan.
hey stan.
Am 22.03.2006 um 13:52 schrieb Soryu:
[...] very nice.
thanks. *proud*
You might have a look here: http://www.ruby-doc.org/stdlib/libdoc/ rss/rdoc/classes/RSS/Converter.html to convert the iso-8859-1 to utf-8 maybe.
I could not get it to work though :)
me neither. but this works: replace the last puts-line by this:
$tidy_html = `echo '#{$html}' | ${TM_TIDY:-tidy} -f /dev/null -i - ascii -asxhtml -wrap 0` puts $tidy_html
sometimes things are easier, than one thinks :).
onother refinement: i recorded a macro that does a backward-search for "<", moves the caret 2 bytes to the right and then calls the command. this way you don't have to have the cursor on the html- element. enjoy!
n.
ps: for the records: the whole command now looks like this:
#!/usr/bin/env ruby # # open element reference on de.selfhtml.org # this is certainly a BAD implementation # works for me, though # any improvements are highly appreciated # niko.
require 'net/http' $tag_name = ENV["TM_CURRENT_WORD"].to_s
# get the elemnte.htm and attribute.htm: Net::HTTP.start( 'de.selfhtml.org', 80 ) do |http| $html_elemente = http.get( "/html/referenz/elemente.htm" ).body $html_attribute = http.get( "/html/referenz/attribute.htm" ).body end
# strip the head and the current tag: $html_elemente.gsub!(/(<!.*<body>).*(<h2><a class="an" name="# {$tag_name}">#{$tag_name}</a></h2>.*?)<h2>.*/m,'\1\2')
# prepend "elemente.htm" to anchor-links: $html_elemente.gsub!(/href="#(.*?")/,'href="elemente.htm#\1')
# the current tag: $html_attribute.gsub!(/.*(<h2><a class="an" name="#{$tag_name}"># {$tag_name}</a></h2>.*?)<h2>.*/m,'\1')
# prepend "elemente.htm" to anchor-links: $html_attribute.gsub!(/href="#(.*?")/,'href="attribute.htm#\1')
# concat the two strings: $html = $html_elemente + $html_attribute
# turn relative links into absolute links (for hrefs and imgs): $html.gsub!(/href="(.*?")/,'href="http://de.selfhtml.org/html/ referenz/\1') $html.gsub!(/src="(.*?")/,'src="http://de.selfhtml.org/html/referenz/ \1')
$tidy_html = `echo '#{$html}' | ${TM_TIDY:-tidy} -f /dev/null -i - ascii -asxhtml -wrap 0`
puts $tidy_html
On 22/3/2006, at 15:50, Niko Dittmann wrote:
$tidy_html = `echo '#{$html}' | ${TM_TIDY:-tidy} -f /dev/null -i - ascii -asxhtml -wrap 0` puts $tidy_html
In that case, you could also call iconv -f iso-8859-1 -t utf-8
But you could also do something like:
puts "<meta http-equiv='Refresh' content='0;URL=#{url}'>"
That should then also save you the trouble of rewriting the relative URLs.
onother refinement: i recorded a macro that does a backward-search for "<", moves the caret 2 bytes to the right and then calls the command. this way you don't have to have the cursor on the html- element. enjoy!
You can also read TM_CURRENT_LINE and TM_LINE_INDEX for the carets position and search back for the tag in code.
hi allan.
Am 22.03.2006 um 16:19 schrieb Allan Odgaard:
On 22/3/2006, at 15:50, Niko Dittmann wrote:
$tidy_html = `echo '#{$html}' | ${TM_TIDY:-tidy} -f /dev/null -i - ascii -asxhtml -wrap 0` puts $tidy_html
In that case, you could also call iconv -f iso-8859-1 -t utf-8
certainly right. thank you. this is a shell-command, right? the ruby- method is "iconv", too, i suppose... couldn't figure out how to get this to work... is this used in any other command?
But you could also do something like:
puts "<meta http-equiv='Refresh' content='0;URL=#{url}'>" That should then also save you the trouble of rewriting the relative URLs.
hm.... but as i include 2 selfhtml-pages (elemtens and attributes) into one page of output i thought it would be a good idea to filter the pages before concating them and just show the documentation for what i lookup... with your method it isn't even possible to include 2 pages into one, is it?
onother refinement: i recorded a macro that does a backward-search for "<", moves the caret 2 bytes to the right and then calls the command. this way you don't have to have the cursor on the html- element. enjoy!
You can also read TM_CURRENT_LINE and TM_LINE_INDEX for the carets position and search back for the tag in code.
hmm... i couldn't figure out how to do this. this would be far more elegant. is there another command where this is used?
sometimes it's frustrating being a nube...
regards, niko.
On 22/3/2006, at 16:52, Niko Dittmann wrote:
In that case, you could also call iconv -f iso-8859-1 -t utf-8
certainly right. thank you. this is a shell-command, right? the ruby-method is "iconv", too, i suppose... couldn't figure out how to get this to work... is this used in any other command?
It’s a shell command, yes. Iconv though exist as a library and is thus available in a few programming languages as iconv as well.
Using it in Ruby to convert from iso-8859-1 should be a matter of:
require 'iconv' ... $html = Iconv.iconv("utf-8", "iso-8859-1", $html)
But you could also do something like:
puts "<meta http-equiv='Refresh' content='0;URL=#{url}'>" That should then also save you the trouble of rewriting the relative URLs.
hm.... but as i include 2 selfhtml-pages (elemtens and attributes) into one page of output i thought it would be a good idea to filter the pages before concating them and just show the documentation for what i lookup... with your method it isn't even possible to include 2 pages into one, is it?
It is not, no. I overlooked that part, sorry.
You can also read TM_CURRENT_LINE and TM_LINE_INDEX for the carets position and search back for the tag in code.
hmm... i couldn't figure out how to do this. this would be far more elegant. is there another command where this is used?
The CSS -> Documentation for Property does something close, but not exactly the same.
Here’s an example which grab the first (when scanning right-to-left) tag to the left of the caret:
line = ENV["TM_CURRENT_LINE"].to_s pos = ENV["TM_LINE_INDEX"].to_i
if m = /\A.{0,#{pos}}<\s*(\w+)/.match(line) then puts "Tag: " + m[1] end
The magic is in the regexp. What it does is, first it ensures the match starts from the beginning of the line (‘\A’), then it matches at most “caret index” characters, and then it matches a tag (‘<\s*(\w +)’), putting the name in capture group 1.
Since the range given (‘{0,#{pos}}’) is greedy, it will match as many characters as possible, which still allow for a tag to be matched.
ok. so here's the final result... errm... result... thanks for the help, allan.
niko.
#!/usr/bin/env ruby # # open element + attribute reference on de.selfhtml.org # # idea: niko dittmann # credits for assisting a complete nube go to # allan odgaard (as always) and stan (soryu) # # i guess, theres still some regex-magic to # be improved (as always :)
require 'iconv' require 'net/http'
line = ENV["TM_CURRENT_LINE"].to_s pos = ENV["TM_LINE_INDEX"].to_i
# this regex gets the first tag left of the caret if m = /\A.{0,#{pos}}<\s*(\w+)/.match(line) then $tag_name = m[1] end
# get the elemnte.htm and attribute.htm: Net::HTTP.start( 'de.selfhtml.org', 80 ) do |http| $html_elemente = http.get( "/html/referenz/elemente.htm" ).body $html_attribute = http.get( "/html/referenz/attribute.htm" ).body end
# strip the head and the current tag: $html_elemente.gsub!(/(<!.*<body>).*(<h2><a class="an" name="# {$tag_name}">#{$tag_name}</a></h2>.*?)<h2>.*/m,'\1\2')
# prepend "elemente.htm" to anchor-links: $html_elemente.gsub!(/href="#(.*?")/,'href="elemente.htm#\1')
# the current tag: $html_attribute.gsub!(/.*(<h2><a class="an" name="#{$tag_name}"># {$tag_name}</a></h2>.*?)<h2>.*/m,'\1')
# prepend "elemente.htm" to anchor-links: $html_attribute.gsub!(/href="#(.*?")/,'href="attribute.htm#\1')
# concat the two strings: $html = $html_elemente + $html_attribute
# turn relative links into absolute links (for hrefs and imgs): $html.gsub!(/href="(.*?")/,'href="http://de.selfhtml.org/html/ referenz/\1') $html.gsub!(/src="(.*?")/,'src="http://de.selfhtml.org/html/referenz/ \1')
$html = Iconv.iconv("utf-8", "iso-8859-1", $html) puts $html