]> mj.ucw.cz Git - ucwmac.git/commitdiff
Separate modules for links and references
authorMartin Mares <mj@ucw.cz>
Tue, 5 Jun 2018 19:41:57 +0000 (21:41 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 5 Jun 2018 19:41:57 +0000 (21:41 +0200)
test-ref.tex
ucw-link.tex [new file with mode: 0644]
ucw-ref.tex

index 996777309ab90441b20c14f3892607ae8677ad4b..cefb5dc687a9d619babe78b03d0ec898a7e8b9e3 100644 (file)
@@ -33,6 +33,6 @@
 
 See also chapter~\chapref{pri} at page \ref{page}{pri}.
 
-Hard link to page~\pagelink{1}.
+Hard link to \linkpage{1}{page~1}.
 
 \bye
diff --git a/ucw-link.tex b/ucw-link.tex
new file mode 100644 (file)
index 0000000..cbe4ec9
--- /dev/null
@@ -0,0 +1,49 @@
+% The UCW Macro Collection: Destinations and links
+% Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
+% -------------------------------------------------------------------------
+
+% Should clickable links be produced?
+\newif\ifclickable
+\clickabletrue
+
+% Common style of all clickable links
+\pdflinkmargin=1pt
+\def\commonlinkargs{height \the\dimexpr\ht\strutbox-0.5pt\relax depth \the\dimexpr\dp\strutbox-0.5pt\relax attr {/C [0 0 0.5] /Border [0 0 2]}}
+
+% Define a PDF destination for the current position at the page
+\def\destpos#1{\ifclickable\pdfdest name {#1} xyz\relax\fi}
+
+% Define a PDF destination for the current page
+\def\destpage#1{\ifclickable\pdfdest name {#1} fit\relax\fi}
+
+% Typeset a clickable link to the given destination
+% \link{dest}{text}
+\def\link#1#2{%
+       \ifclickable
+               \pdfstartlink\commonlinkargs goto name {#1}\relax
+               #2%
+               \pdfendlink\relax
+       \else
+               #2%
+       \fi
+}
+
+% Typeset a clickable link to the given page number
+% (This does not use named destinations. We use it in tables of contents and indices,
+% where absolute page numbers are known from other sources.)
+% \linkpage{page}{text}
+\def\linkpage#1#2{\ifclickable\pdfstartlink\commonlinkargs goto page #1 {/Fit}\relax #2\pdfendlink\else #2\fi}
+
+% Typeset a clickable URL
+% Currently, all weird characters must be properly escaped.
+\def\url#1{%
+       \leavevmode
+       \ifclickable
+               \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(#1) >>}\relax
+       \fi
+       \xurl #1^^X%
+       \ifclickable
+               \pdfendlink\relax
+       \fi
+}
+\def\xurl#1:#2#3^^X{{\I #1:#2\ifx#2/\kern-0.1em\fi#3}}
index e90b642e4b81120883177309e03dce7ea0e43792..74143e212b2825dd93013bdf0e1307a1ce917a0a 100644 (file)
@@ -2,9 +2,7 @@
 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
 % -------------------------------------------------------------------------
 
-% Should clickable links be produced?
-\newif\ifclickable
-\clickabletrue
+\input ucw-link.tex
 
 % We maintain a collection of objects. Each object has:
 %    - type (namespace -- e.g., chapter)
@@ -47,9 +45,7 @@
        \else
                \edef\currentid{#1\optarg}%
                \writeid{\currentid}{#2}%
-               \ifclickable
-                       \pdfdest name {\currentid} xyz\relax
-               \fi
+               \destpos{\currentid}%
        \fi
 }
 
        {\ifx\bo\undefined\bf\else\bo\fi ??}%
        \immediate\write16{*** Warning: Reference #1 undefined ***}%
 \else
-       \ifclickable
-               \pdfstartlink\commonlinkargs goto name {#1#2}\relax
-       \fi
-       #3{\csname id:#1#2\endcsname}%
-       \ifclickable
-               \pdfendlink\relax
-       \fi
+       \link{#1#2}{#3{\csname id:#1#2\endcsname}}%
 \fi
 }
 
-% Common style of all clickable links
-\pdflinkmargin=1pt
-\def\commonlinkargs{height \the\dimexpr\ht\strutbox-0.5pt\relax depth \the\dimexpr\dp\strutbox-0.5pt\relax attr {/C [0 0 0.5] /Border [0 0 2]}}
-
 % Typeset a link to identifier #2 of type #1
 \def\ref#1#2{\reflink{#1}{#2}\relax}
 
 % Define an identifier of type page pointing to the current page
-\def\pageid#1{\delayedwriteid{page#1}{\the\count0}\ifclickable\pdfdest name {page#1} fit\relax\fi}
+\def\pageid#1{\delayedwriteid{page#1}{\the\count0}\destpage{page#1}}
 \def\pageref{\ref{page}}
-
-% Typeset a reference to the given page number (used in tables of contents and indices,
-% where we know the page number from other sources).
-\def\pagelink#1{\ifclickable\pdfstartlink\commonlinkargs goto page #1 {/Fit}\relax #1\pdfendlink\else #1\fi}
-
-% Typeset a clickable URL
-% Currently, all weird characters must be properly escaped.
-\def\url#1{%
-       \leavevmode
-       \ifclickable
-               \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(#1) >>}\relax
-       \fi
-       \xurl #1^^X%
-       \ifclickable
-               \pdfendlink\relax
-       \fi
-}
-\def\xurl#1:#2#3^^X{{\I #1:#2\ifx#2/\kern-0.1em\fi#3}}