473,789 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1827
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*****@mypetpr ogrammer.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.ne t.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.protot ype.call() and
Function.protot ype.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.de mon.co.uk>
Jan 6 '08 #4
On Jan 6, 8:47*am, Thomas 'PointedEars' Lahn <PointedE...@we b.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.ne t.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.javas cript message <47************ **********@road runner.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(thoug h 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(text Nodes, 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(NumberOf Lines).length ; ??

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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(thoug h 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.sp lit("\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(text Nodes, 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(NumberOf Lines).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(thoug h 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.sp lit("\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.javas cript message <47************ ***********@roa drunner.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(thoug h 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.sp lit("\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.demo n.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
1797
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
2939
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 line numbers, with no effort required from me. Is this a standard offering in .Net, or is rolling your own line numbers still the way to go? Also, my components at the moment are being compiled in debug mode. Will line numbers still be available...
2
1557
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 possible and also ensuring that it fails gracefully. So, I'm trying to use the Erl property of the Err object so the users get a line number as part of the error if things go wrong to make troubleshooting easier.
3
12619
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 bindingsource.Add method to get the rows into the grid. That all works fine, but at runtime, the grid wont' allow me to sort. I have used he smarttage to edit the columns and made sure that the sortmode is set to "Automatic", but when I click the column...
7
1962
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 time as well. My XML: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
1
1757
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 automatic numbering of section like in latex using \section*{} instead of \section{} ? Or should I customize the XSLT with a new kind of section
58
4691
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 the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
0
2080
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
1467
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 to do is make an automatic filtering and labeling method for my table. The reason for this is because I m handling more than 500 records and labeling them would take me forever, so I started with what I can understand, which is seeking 5 records...
0
9665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.