[TxMt] Suggestion to improve doctohtml.rb => relative line numbering

Hans-Jörg Bibiko bibiko at eva.mpg.de
Mon Jul 28 16:06:29 UTC 2008

Dear all,

I'm just writing a a tiny bundle
'Copy Formatted as'

I was inspired by the bundle Copy as RTF by Max Muermann (kindly  
hosted at github by Dr Nic Williams) [I found it with GetBundles ;)].
This bundle works fine for RTF but it lacks UTF-8 support. Thus I  
went an other way to achieve that and it works perfectly also for TeX ;)

But back to my suggestion.

It often occurs to me that I'm to write a tiny paper, article or  
whatever about a piece of source code I wrote. For that purpose I  
often need doctohtml.rb with line numbering. Fine. But one thing I  
cannot do with it. If I write something about a piece of code between  
line 10 to 20 and I want to refer to line number in my text the  
default doctohtml.rb produces line numbers between 1 to 10. Due to  
that fact I thought it would be nice to have an option to produce  
relative line numbers.

I modified doctohtml.rb a bit to achieve that.

Here's the diff (also attached to that mail):

--- untitled
+++ (clipboard)
@@ -189,10 +189,10 @@
  	return lines.join("\n")

-def number(str)
+def number(str, rel_numbering = false)
  	# number each line of input
  	lines = str.split(/\n/)
-	n = 0
+	n = (rel_numbering) ? ENV['TM_INPUT_START_LINE'].to_i - 1 : 0
  	lines.each do | line |
  		n += 1
  		line.gsub!(/^(<\/span>)?/, "\\1<span class='linenum'>#{ sprintf("% 
5d", n) }</span> ")
@@ -255,7 +255,7 @@

-	code_html = number(code_html) if opt[:line_numbers]
+	code_html = number(code_html, rel_numbering = opt 
[:relative_numbering]) if opt[:line_numbers]

  	html << "<pre class=\"textmate-source"
  	html << " #{theme_class}" unless theme_class.empty?

By doing so one can call document_to_html like that:

document_to_html( STDIN.read, {:relative_numbering =>  
true, :line_numbers => true, :include_css => true }

If one leaves the new option out the line numbering starts at 1.

Hopefully this tiny improvement will be accepted soon ;)



-------------- next part --------------
A non-text attachment was scrubbed...
Name: doctohtml.rb.diff
Type: application/octet-stream
Size: 675 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20080728/112cc99a/attachment.diff>

More information about the textmate mailing list