Welcome, Guest. Please Login
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
Pages: 1
Send Topic Print
In-page anchor links (Read 1915 times)
Mark Anderson
YaBB Administrator

User - not staff!

Posts: 5689
Southsea, UK
In-page anchor links
Jun 7th, 2010, 10:56am
Les Orchard just posted a very nice HTML Export tutorial (includes a link to the source TBX). At the end of the article, he talks about about in-page anchors, which led led me to some experimentation trying to figure out some simple macro for a link to embedded concept - something I tried a fear years back and abandoned as too difficult. The code may look complex but that's only to make it simple for the user.

Before we go on, the process assumes that you:
  • understand the basics of HTML named anchors (if not, look it up on Google)
  • that you have added to your TBX a Number-type attribute with the 'sequential' option ticked. In my case it is called $MySeq - by all means use different and adjust any code samples below accordingly. These get used to give our anchors and links to then a unique ID (UID).
  • you know what pages are included where.  TB can't figure the latter out for you (for some good under-the-hood reasons), so this is a technique involving some planning on the part of the user
One happy discovery is that since I last played with anchors (i.e. of the pagename.html#anchorname varierty) that modern browsers can target any id attribute - as the latter must (for validation purposes) but unique within a given page.  This is nice as we don't need <a name"anchorname"></a> mark-up but can simply add an id attribute, e.g. to a heading tag. Testing in Safari, Firefox and IE7/8 all found the correct target using the latter method.

Why do I prefix my 'UID' value with an 'x'?  Because, HTML id attribute values cannot start with a number so I'm simply putting a single letter in front to give a validating id attribute value. IOW, id="x23" vs. id="23".

So in exporting included page content, might mark its heading thus:

<h^value($OutlineDepth)^ id="x^value($MySeq)^">^title^</h^value($OutlineDepth)^>

If note of $MySeq value 42 is exported in page example.html, we'd need to link to "example.html#x42". So how to achieve the latter?

A macro to the rescue, code here broken out into lines for clarity (blue is basic text the rest is export code):

<a href="

The above yields links like:
  <a href="#x45">See more</a>
  <a href="../Courses/Module4.html#x45">Lesson 7</a>

The macro takes one mandatory argument and 2 optional ones:
    [1] The $Name of the target note
    [2] The $Name of the note whose HTML page includes the above
    [3] The text of the (clickable) anchor text to be use in the link
As in:
  ^do(anchorlink,target note,source note[,clickable text])^

Thus to link to note "Phase 2" in the same HTML page:
  ^(anchorlink,Phase 2)^
Or, to link to note "Phase 2", in the exported page from note "Export Demo":
  ^(anchorlink,Phase 2,Export Demo)^

Or, to link to note "Phase 2", in the exported page from note "Export Demo" via the link text "Go to Phase 2":
  ^(anchorlink,Phase 2,Export Demo, Go to Phase 2)^

Basically the macro code asks "Is there a parameter #2 and if there is, as long as its ^url()^ value isn't '#' (effectively a null value) use the ^url^ output as a prefix to the id attribute value. The UID number comes from $MySeq for parameter #1. The other conditional checks for a third parameter; if found that forms the link anchor text, otherwise the link text is the target note's name.

The main thing the user needs to know (because they understand the HTML export they planned!) is the parent HTML page within which the targeted content is encapsulated.

Here's the whole macro (paste all as one line if pasting to a TB macro):

<a href="^if($2)^^if(^not(^equal(^url($2)^,#)^)^)^^url($2)^^endIf^^endIf^#x^getFor(

Ideally, a TB stamp could paste in a boilerplate syntax of the necessary ^do()^ for which to then edit the parameter values. however, stamps can't be used within $Text. No idea why stamps into $Text aren't allowed, but as at v5.5.0 they aren't (feature request).

I'll leave it to someone else to validate this using Unicode note names - but it certainly works with normal English text.

All this is yak shaving en route to making PDFs with internal links. Anyone know a tool that will 'print' styled HTML to PDF inlcuding in-page links as PDF links?
Back to top
« Last Edit: Jun 07th, 2010, 1:58pm by Mark Anderson »  

Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print