Re: [asciidoc-discuss] [PATCH] permalink support
VMiklos wrote: 2007/8/31, Stuart Rackham <[EMAIL PROTECTED]>: Thanks very much for the patch, I'll put it into the next release. great :) At this stage I'll make auto-generated section IDs optional (enabled by defining the `-a sectids` attribute). is asciidoc tracked in some version control system? is it public? i just could then patch asciidoc with the same patch that will be included in the next asciidoc release (i suppose you implemented this -a sectids feature, probably it's trivial for you, but not for me :) ) The svn repository is not on the Net, but I've attached the full patch for 8.2.2. thanks, - VMiklos ___ Asciidoc-discuss mailing list [email protected] http://metaperl.com/cgi-bin/mailman/listinfo/asciidoc-discuss Cheers, Stuart Index: asciidoc.py === --- asciidoc.py (revision 128) +++ asciidoc.py (working copy) @@ -9,7 +9,7 @@ import sys, os, re, string, time, traceback, tempfile, popen2, codecs, locale from types import * -VERSION = '8.2.2' # See CHANGLOG file for version history. +VERSION = '8.2.3b1' # See CHANGLOG file for version history. #--- # Program onstants. @@ -1130,7 +1130,7 @@ def init_attrs(self): # Set implicit attributes. d = time.localtime(time.time()) -self.attributes['localdate'] = time.strftime('%d-%b-%Y',d) +self.attributes['localdate'] = time.strftime('%Y-%m-%d',d) s = time.strftime('%H:%M:%S',d) if time.daylight: self.attributes['localtime'] = s + ' ' + time.tzname[1] @@ -1676,7 +1676,8 @@ class Section: '''Static methods and attributes only.''' -endtags = [] # Stack of currently open section (level,endtag) tuples. +endtags = [] # Stack of currently open section (level,endtag) tuples. +ids = [] # List of already used ids. def __init__(self): raise AssertionError,'no class instances allowed' def savetag(level,etag): @@ -1689,6 +1690,31 @@ writer.write(Section.endtags.pop()[1]) document.level = level setlevel = staticmethod(setlevel) +def gen_id(title): +''' The normalized value of the id attribute is an NCName according to +the 'Namespaces in XML' Recommendation: +NCName ::= NCNameStartChar NCNameChar* +NCNameChar ::= NameChar - ':' +NCNameStartChar ::= Letter | '_' +NameChar::= Letter | Digit | '.' | '-' | '_' | ':' +''' +id = re.sub(r'[^a-zA-Z0-9]+', '_', title).strip('_').lower() +# Prefix with underscore to ensure a valid id start character and to +# ensure the id does not clash with existing document id's. +id = '_' + id +i = 1 +while True: +if i == 1: +tid = id +else: +tid = '%s_%d' % (id, i) +if tid not in Section.ids: +Section.ids.append(tid) +return tid +else: +tid = id +i += 1 +gen_id = staticmethod(gen_id) def translate(): assert Lex.next() is Title prev_sectname = Title.sectname @@ -1712,6 +1738,10 @@ warning('section title out of sequence: ' 'expected level %d, got level %d' % (document.level+1, Title.level)) +if not document.attributes.get('sectids') is None \ +and 'id' not in AttributeList.attrs: +# Generate ids for sections. +AttributeList.attrs['id'] = Section.gen_id(Title.dict['title']) Section.setlevel(Title.level) Title.dict['sectnum'] = Title.getnumber(document.level) AttributeList.consume(Title.dict) Index: asciidoc.conf === --- asciidoc.conf (revision 128) +++ asciidoc.conf (working copy) @@ -12,6 +12,7 @@ newline=\r\n [attributes] +sectids= iconsdir=./images/icons encoding=UTF-8 quirks= Index: xhtml11.conf === --- xhtml11.conf(revision 128) +++ xhtml11.conf(working copy) @@ -300,25 +300,25 @@ # Document sections. [sect0] -{id?}{title} +{title} | [sect1] -{id?}{numbered?{sectnum} }{title} +{numbered?{sectnum} }{title} | [sect2] -{id?}{numbered?{sectnum} }{title} +{numbered?{sectnum} }{title} | [sect3] -{id?}{numbered?{sectnum} }{title} +{numbered?{sectnum} }{title} | [sect4] -{id?}{title} +{title} | [header] Index: CHANGELOG.txt === --- CHANGELOG.txt (revision 128) +++ CHANGELOG.txt (working copy) @@ -1,6 +1,17 @@ AsciiDoc ChangeLog == +Version 8.2.3
Re: [asciidoc-discuss] [PATCH] permalink support
2007/8/31, Stuart Rackham <[EMAIL PROTECTED]>: > Thanks very much for the patch, I'll put it into the next release. great :) > At this stage I'll make auto-generated section IDs optional (enabled > by defining the `-a sectids` attribute). is asciidoc tracked in some version control system? is it public? i just could then patch asciidoc with the same patch that will be included in the next asciidoc release (i suppose you implemented this -a sectids feature, probably it's trivial for you, but not for me :) ) thanks, - VMiklos ___ Asciidoc-discuss mailing list [email protected] http://metaperl.com/cgi-bin/mailman/listinfo/asciidoc-discuss
Re: [asciidoc-discuss] [PATCH] permalink support
Thanks very much for the patch, I'll put it into the next release. I
installed and regenerated the AsciiDoc User Guide, works well with a
couple of easily fixed exceptions:
1. If two or more elements with the same ID already exist outside the
generated titles you will get an XHTML validity error. For
example IDs generated by BlockId elements, in the AsciiDoc headers
or footers or filter generated. In this example the 'header' ID is
generated by the AsciiDoc header:
asciidoc.css-embedded.html:965: element h3: validity error : ID
header already defined
5.2. Header
2. If the title does not start with an underscore or letter invalid
xhtml will result, here's an (admitedly contrived) example where
the title '-Introduction-' results in an error:
asciidoc.css-embedded.html:395: element h2: validity error :
Syntax of
value for attribute id of h2 is not valid
1. -Introduction-
3. Trailing non-alphanumeric characters in title results in trailing
dashes in ID (not a validity error, just looks funny) e.g.
'Line Breaks (HTML/XHTML)' -> 'line-breaks-html-xhtml-'
Issues 1 and 2 can be fixed by prepending an underscore to the
generated ID (at fist sight it's a little ugly but it is a
straight-forward unambiguous solution). Issue 3 is trivial. Here's the
code:
def gen_id(title):
''' The normalized value of the id attribute is an NCName
according to
the 'Namespaces in XML' Recommendation:
NCName ::= NCNameStartChar NCNameChar*
NCNameChar ::= NameChar - ':'
NCNameStartChar ::= Letter | '_'
NameChar::= Letter | Digit | '.' | '-' | '_' | ':'
'''
id = re.sub(r'[^a-zA-Z0-9.-]+', '_', title).strip('_').lower()
# Prefix with underscore to ensure a valid id start character and to
# ensure the id does not clash with existing document id's.
id = '_' + id
i = 1
while True:
if i == 1:
tid = id
else:
tid = '%s_%d' % (id, i)
if tid not in Section.ids:
Section.ids.append(tid)
return tid
else:
tid = id
i += 1
gen_id = staticmethod(gen_id)
The only other change I made is to use underscores instead of dashes,
it makes the rest of the ID consistent with the prefix and I find
underscores easier on the eye for long titles.
At this stage I'll make auto-generated section IDs optional (enabled
by defining the `-a sectids` attribute).
--
Cheers, Stuart
VMiklos wrote:
> hi,
>
> i have the following problem: when creating big documents, the anchor
> of a chapter looks like "#toc34" and so on. this can be problematic
> when you give the link to someone and later you insert a chapter
> before "toc34"
>
> so here is what i did:
>
> http://frugalware.org/~vmiklos/patches/asciidoc-8.2.2-permalink.diff
>
> of corse it adds these ids only in case there is no id defined by the user
>
> what do you think about it?
>
> thanks,
> - VMiklos
>
> ___
> Asciidoc-discuss mailing list
> [email protected]
> http://metaperl.com/cgi-bin/mailman/listinfo/asciidoc-discuss
>
___
Asciidoc-discuss mailing list
[email protected]
http://metaperl.com/cgi-bin/mailman/listinfo/asciidoc-discuss
