473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Automatic line numbering on PRE elements - Solved, sort of

When I couldn't find it on-line by Googling,
I spent all last night working it out. And it
works, Yea! All you do is mark the PRE sections
you want numbered with a "marker" style like this:

....
<style ...>
..numbered {}
</style>
....
<pre class="numbered">
These lines get numbered automatically.
I just enter the text;
JavaScript does the rest!
</pre>
....

Of course this may have been easy and solutions posted
long ago, but I didn't find any. CSS 3 promises
to do this with ::line-marker pseudo selector, but
that seems a long way off.

Only problem is my JavaScript skills are all rusty.
While it works great in FireFox it doesn't work in
IE. (OK, that's not the only problem. My inefficient
code sometimes causes a "flicker" when the page loads.
And it would be nice to be able to copy the
text without the numbers. But really I just would be
happy to have it work in most modern browsers.)

Would it be reasonable to post the code here, and have
some of you experts fix it up so it works in all
browsers?

-Wayne
Dec 29 '07 #1
8 1808
Wayne said:
[snip]
Would it be reasonable to post the code here, and have
some of you experts fix it up so it works in all
browsers?

-Wayne
Wayne,

While I am not an expert, I would like very much to see the code, and to
help you get it worked out. I could use the practice.

All the best,
~A!
--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 29 '07 #2
Ed
Wayne wrote:
I look forward to a vastly improved version that works on
IE and other browsers too! Thanks for any help.
In IE, text nodes will contain \r instead of \n.
Dec 29 '07 #3
RobG wrote:
On Jan 6, 7:39 pm, David Mark <dmark.cins...@gmail.comwrote:
>On Jan 6, 4:02 am, RobG <rg...@iinet.net.auwrote:
>>This will return a NodeList to the calling function, but if a
className was specified, the function will return an Array. These are
quite different objects, the function should reliably return one or
the other (probably an Array), always.
In a perfect world, yes. However, I have never found an efficient way
to convert a nodelist to an array,

Depending on the browser, it may be possible to extend NodeList with
the required methods (untested and I expect it will only work in a few
browsers).
Depending on the browser, especially the available script engine, it would
be possible to use methods of Array.prototype, without the error-prone
augmentation of the host object, by using Function.prototype.call() and
Function.prototype.apply().

http://PointedEars.de/scripts/es-matrix/#f
Your signature delimiter is broken because of a bug in Google Groups.
I suggest you do not to use your common signature when using that Web interface.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jan 6 '08 #4
On Jan 6, 8:47*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
RobG wrote:
On Jan 6, 7:39 pm, David Mark <dmark.cins...@gmail.comwrote:
On Jan 6, 4:02 am, RobG <rg...@iinet.net.auwrote:
This will return a NodeList to the calling function, but if a
className was specified, the function will return an Array. *These are
quite different objects, the function should reliably return one or
the other (probably an Array), always.
In a perfect world, yes. *However, I have never found an efficient way
to convert a nodelist to an array,
Depending on the browser, it may be possible to extend NodeList with
the required methods (untested and I expect it will only work in a few
browsers).

Depending on the browser, especially the available script engine, it would
be possible to use methods of Array.prototype, without the error-prone
Like using slice on an arguments object? Doesn't work for nodelists
(at least not in the browsers I tested.)
Jan 6 '08 #5
In comp.lang.javascript message <47**********************@roadrunner.com
>, Sun, 6 Jan 2008 01:27:01, Wayne <no****@all4me.invalidposted:
>The two parts that *may* need improvement are:
(1) Setting of the newline character used in a PRE,
which AFAIK is '\n' except in IE, so I use an IE
conditional comment to set it to '\r'. Is this
the best way to detect the EOL character used?
One should be able to use .split with a suitable RegExp to determine the
number of lines; I do this in button Indt in <URL:http://www.merlyn.demo
n.co.uk/js-quick.htm(though ISTR one must be careful, and the code for
button Pack needs some adjustment to accommodate Safari 3.0.4 in XP
perfectly).

If the FAQ dealt with RegExps adequately, it would include information
on splitting text into lines cross-browser.
var width = 1 + Math.ceil( Math.LOG10E
* Math.log( countLines(textNodes, newline) ) );
That may work reliably, but it's a bad idea to use intrinsically-float
arithmetic for what must be essentially an integer matter.

How about var width = 1 + String(NumberOfLines).length ; ??

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 6 '08 #6
Dr J R Stockton wrote:
...
>(1) Setting of the newline character used in a PRE,
which AFAIK is '\n' except in IE, so I use an IE
conditional comment to set it to '\r'. Is this
the best way to detect the EOL character used?

One should be able to use .split with a suitable RegExp to determine the
number of lines; I do this in button Indt in <URL:http://www.merlyn.demo
n.co.uk/js-quick.htm(though ISTR one must be careful, and the code for
button Pack needs some adjustment to accommodate Safari 3.0.4 in XP
perfectly).
Your code:

Lines = G.Code.value.split("\n") // Code to Lines

won't work for IE for a PRE element, which doesn't use '\n'
but rather '\r'.

(While I appreciate the advice many have posted here about using
a regular expression for this, it seems nobody can craft one!)
If the FAQ dealt with RegExps adequately, it would include information
on splitting text into lines cross-browser.
I agree; I wish someone who knows how would post or add that!
>

> var width = 1 + Math.ceil( Math.LOG10E
* Math.log( countLines(textNodes, newline) ) );

That may work reliably, but it's a bad idea to use intrinsically-float
arithmetic for what must be essentially an integer matter.

How about var width = 1 + String(NumberOfLines).length ; ??
*Much* better, thanks!

I will also work on eliminating the imported code. Sadly the
CWR website isn't working for me today. I guess I'll try
it myself; even if it doesn't work well I know I'll learn
something.

-Wayne
Jan 7 '08 #7
Wayne wrote:
Dr J R Stockton wrote:
>...
>>(1) Setting of the newline character used in a PRE,
which AFAIK is '\n' except in IE, so I use an IE
conditional comment to set it to '\r'. Is this
the best way to detect the EOL character used?

One should be able to use .split with a suitable RegExp to determine the
number of lines; I do this in button Indt in <URL:http://www.merlyn.demo
n.co.uk/js-quick.htm(though ISTR one must be careful, and the code for
button Pack needs some adjustment to accommodate Safari 3.0.4 in XP
perfectly).

Your code:

Lines = G.Code.value.split("\n") // Code to Lines

won't work for IE for a PRE element, which doesn't use '\n'
but rather '\r'.

(While I appreciate the advice many have posted here about using
a regular expression for this, it seems nobody can craft one!)
>If the FAQ dealt with RegExps adequately, it would include information
on splitting text into lines cross-browser.

I agree; I wish someone who knows how would post or add that!
I posted one in my first response, it may also be found in the archives
by searching for terms like "IE textarea newline return" or simlar.
[...]
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Jan 7 '08 #8
In comp.lang.javascript message <47***********************@roadrunner.co
m>, Sun, 6 Jan 2008 22:03:21, Wayne <no****@all4me.invalidposted:
>Dr J R Stockton wrote:
>...
>>(1) Setting of the newline character used in a PRE,
which AFAIK is '\n' except in IE, so I use an IE
conditional comment to set it to '\r'. Is this
the best way to detect the EOL character used?
One should be able to use .split with a suitable RegExp to determine
the
number of lines; I do this in button Indt in <URL:http://www.merlyn.demo
n.co.uk/js-quick.htm(though ISTR one must be careful, and the code for
button Pack needs some adjustment to accommodate Safari 3.0.4 in XP
perfectly).

Your code:

Lines = G.Code.value.split("\n") // Code to Lines

won't work for IE for a PRE element, which doesn't use '\n'
but rather '\r'.
Yes, but, as far as I've so far tested, it's suitable for the textarea
element that I use it on.
>(While I appreciate the advice many have posted here about using
a regular expression for this, it seems nobody can craft one!)
>If the FAQ dealt with RegExps adequately, it would include information
on splitting text into lines cross-browser.

I agree; I wish someone who knows how would post or add that!
ISTM that, to do it properly, the first thing is to make a list of all
the treatments-applied-to-elements that can yield multi-line text
strings; and then for each of those to list the possible line separators
(giving consideration to VT, FF, PS, etc.). Then one will have the
information necessary to design the splitting routine or routines.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Jan 7 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mike Dickens | last post by:
hi, suppose i have: <a> <b i="Y" j="aaaa"/> <c i="N" j="bbbb"/> <d i="Y" j="cccc"/> <e i="N" j="dddd"/> <f i="N" j="eeee"/> <g i="Y" j="ffff"/>
3
by: Jim Bancroft | last post by:
Hi all, In VB6 I used a 3rd party tool for line numbering my source code, to help with debugging. However, in experimenting with VB .Net I've noticed that my exceptions automatically provide...
2
by: Ben | last post by:
Hello all After years of playing developing stuff for fun and personal use I'm finally developing an app that I am intending to sell. As such, I'm trying to make it as reliable and stable as...
3
by: mac | last post by:
I have a datagridview that I populate with the result of a web service. The webservice sends multiple rows back with each row as one element of an array. I use a for loop and the...
7
by: pstachy | last post by:
Hi all, I'm having problem with XSLT transformation concerned with sort and numberings at a time. I wish to have my registries sorted alfabetically and would like them to have numberings at a...
1
by: Michael Doubez | last post by:
Hello, I want to mix in the same document Parts that have automatically numbered sections and parts which section are not numbered. Is there any out of the box way to locally suppress the...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
0
by: [david] | last post by:
http://pyserial.sourceforge.net/ "port numbering starts at zero, no need to know the port name in the user program" But the implementation in SerialWin32 is just (Portnum +1)
0
by: cephal0n | last post by:
Hi All! I have two table tblHome1, contains all the unique PinNo and tblHome2 contains a duplicated PinNo and description. I put an automatic numbering (ProdID) and set it as my index. What I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.