sequential numbering

56 views
Skip to first unread message

Ken Lanxner

unread,
Apr 11, 2012, 6:27:58 PM4/11/12
to BBEdit Talk
Hi. I have a long definition list and need to number each dt so that

<dt>first item</dt>
<dd>first definition</dd>

<dt>second item</dt>
<dd>second definition</dd>

becomes

<dt>1. first item</dt>
<dd>first definition</dd>

<dt>2. second item</dt>
<dd>secind definition</dd>

and so on. What would be my best approach? Each dt has multiple
dds in the actual document.

Thanks.

Ken

--
Orange County Website Design
http://simplelives.com

Ronald J Kimball

unread,
Apr 11, 2012, 9:25:26 PM4/11/12
to bbe...@googlegroups.com
On Wed, Apr 11, 2012 at 03:27:58PM -0700, Ken Lanxner wrote:
> Hi. I have a long definition list and need to number each dt so that
>
> <dt>first item</dt>
> <dd>first definition</dd>
>
> <dt>second item</dt>
> <dd>second definition</dd>
>
> becomes
>
> <dt>1. first item</dt>
> <dd>first definition</dd>
>
> <dt>2. second item</dt>
> <dd>secind definition</dd>
>
> and so on. What would be my best approach? Each dt has multiple
> dds in the actual document.
>

How about using the CSS content property? For example:

.list {
counter-reset: my-counter;
}
.list dt:before {
content: counter(my-counter) ". ";
counter-increment: my-counter;
}


If you'd rather insert the numbers into the HTML, you would need a script
of some kind. Here's a simple one in Perl, which also replaces existing
numbering, if any:

#!perl -p
s/(<dt[^>]*>)(\d+\. )?/$1 . ++$i . ". "/ige;

Ronald

Ken Lanxner

unread,
Apr 11, 2012, 11:04:06 PM4/11/12
to bbe...@googlegroups.com
Yes! Thanks. For some reason, I had never before come across
counter-increment. That will work perfectly for me.

Ken

r...@tamias.net (Ronald J Kimball) wrote on 4/11/12 6:25 PM

Robert A. Rosenberg

unread,
Apr 11, 2012, 11:40:36 PM4/11/12
to bbe...@googlegroups.com
At 20:04 -0700 on 04/11/2012, Ken Lanxner wrote about Re: sequential numbering:

>Yes! Thanks. For some reason, I had never before come across
>counter-increment. That will work perfectly for me.
>
>Ken

Just remember that using counter-increment assumes that the user has
a browser that supports this tag. A better solution that does not
rely on the user having a compliant browser is to have the page
generated via PHP (so you can generate the counter on the fly. All
that is needed is to replace the <dt> tags with a php function call
that increments a counter and then outputs <dt>$Counter&nbsp;.

Ken Lanxner

unread,
Apr 12, 2012, 1:02:05 AM4/12/12
to bbe...@googlegroups.com
Thanks, Robert. Sensible suggestion. As it happens, these lists
won't be on the web and we are covered as far as browser support.

Ken

rar...@banet.net (Robert A. Rosenberg) wrote on 4/11/12 8:40 PM

--

Robert A. Rosenberg

unread,
Apr 13, 2012, 1:22:00 AM4/13/12
to bbe...@googlegroups.com
At 22:02 -0700 on 04/11/2012, Ken Lanxner wrote about Re: sequential numbering:

>Thanks, Robert. Sensible suggestion. As it happens, these lists
>won't be on the web and we are covered as far as browser support.

No problem. In that case, my comment is not applicable to your
situation. I tend to look for hidden assumptions in other replies (as
in this case) and point them out so the final solution is a fit.
There is nothing worse than to make the effort to implement a
solution only to find that there is a caveat on its usage that was
not mentioned. This habit, as in this case, causes me to make
comments that are unnecessary. OTOH, someone who is lurking here who
has a similar need my find my warning of use in avoiding a pitfall of
assuming that there is no hidden caveats to the counter-increment
method.

>
>Ken
>
>rar...@banet.net (Robert A. Rosenberg) wrote on 4/11/12 8:40 PM
>
>>At 20:04 -0700 on 04/11/2012, Ken Lanxner wrote about Re: sequential
>>numbering:
>>
>>>Yes! Thanks. For some reason, I had never before come across
>>>counter-increment. That will work perfectly for me.
>>>
>>>Ken
>>
>>Just remember that using counter-increment assumes that the user has a
>>browser that supports this tag. A better solution that does not rely
>>on the user having a compliant browser is to have the page generated
>>via PHP (so you can generate the counter on the fly. All that is
>>needed is to replace the <dt> tags with a php function call that
>>increments a counter and then outputs <dt>$Counter&nbsp;.
>
>--
>Orange County Website Design
>http://simplelives.com
>

>--
>You received this message because you are subscribed to the "BBEdit
>Talk" discussion group on Google Groups.
>To post to this group, send email to bbe...@googlegroups.com
>To unsubscribe from this group, send email to
>bbedit+un...@googlegroups.com
>For more options, visit this group at
><http://groups.google.com/group/bbedit?hl=en>
>If you have a feature request or would like to report a problem,
>please email "sup...@barebones.com" rather than posting to the
>group.
>Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>


RobS

unread,
May 21, 2012, 8:41:50 AM5/21/12
to bbe...@googlegroups.com
On Wednesday, 11 April 2012 22:25:26 UTC-3, Ronald J Kimball wrote:
If you'd rather insert the numbers into the HTML, you would need a script

of some kind.  Here's a simple one in Perl, which also replaces existing
numbering, if any:

#!perl -p
s/(<dt[^>]*>)(\d+\. )?/$1 . ++$i . ". "/ige;


I have a similar problem but have no experienece with PERL, so I'm finding it difficult to modify this bit of code to suit. In my case I have a series of lines like this...

<..the start of an href here..>[+]</a>
<..the start of an href here..>[+]</a>

... and I want to replace each of the plus signs with a sequential number, starting with 1, so they end up like this...

<..the start of an href here..>[1]</a>
<..the start of an href here..>[2]</a>

There are about 40 altogether, so more than one digit is required for most of them. With only 40 I could, of course, do it manually, but I'd learn nothing from that. How would PERL handle this?

Thanks,

Rob

Ronald J Kimball

unread,
May 21, 2012, 3:29:39 PM5/21/12
to bbe...@googlegroups.com
Here's how the substitution might be modified for your use case:

#!perl -p
s,(<a\s[^>]+>\[)(?:\+|\d+)(\]</a>),$1 . ++$i . $2,ige;

The regex now has three parts:

(<a\s[^>]+>\[) matches the opening A tag and the left square bracket.
(?:\+|\d+) matches either a plus sign or some digits, to replace
existing numbering as well.
(\]</a>) matches the right square bracket and the closing A tag.

The replacement combines the two captured parts with the value of an
incremented variable.

Ronald

RobS

unread,
May 22, 2012, 8:21:05 PM5/22/12
to bbe...@googlegroups.com
Thanks very much for that Ronald. It will be tucked comfortably into my scripts folder shortly.

Rob
Reply all
Reply to author
Forward
0 new messages