From 5c7e482d0234e6692cb685cccd78b664f1790322 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 5 Jun 2018 22:09:07 +0200 Subject: [PATCH] ucw-link: Better macros for URLs and URL links --- test-ref.tex | 4 +++- ucw-link.tex | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/test-ref.tex b/test-ref.tex index 13f9e1c..95915ab 100644 --- a/test-ref.tex +++ b/test-ref.tex @@ -27,7 +27,9 @@ \chapter[sec]{Caput secunda} -\url{http://pruvodce.ucw.cz/errata.html} +\url{http://pruvodce.ucw.cz/errata%21.html} + +\linkurl{http://pruvodce.ucw.cz/}{See the web page.} \chapter[ter]{Caput tertia} diff --git a/ucw-link.tex b/ucw-link.tex index 58f498f..ef89593 100644 --- a/ucw-link.tex +++ b/ucw-link.tex @@ -37,15 +37,34 @@ \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{% +% \url{http://example.com/} +\def\url{\begingroup\allowurlchars\urlaux} +\def\urlaux#1{\linkurlaux{#1}{\displayurl #1^^X}} + +% Typeset a clickable link to the given URL +% \linkurl{http://example.com/}{text} +\def\linkurl{\begingroup\allowurlchars\linkurlaux} +\def\linkurlaux#1#2{\endgroup \leavevmode \ifclickable \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(#1) >>}\relax \fi - \xurl #1^^X% + #2% \ifclickable - \pdfendlink\relax + \pdfendlink \fi } -\def\xurl#1:#2#3^^X{{\I #1:#2\ifx#2/\kern-0.1em\fi#3}} + +% Catcode each special character valid in URL to 'other' +\def\allowurlchars{\catcode`\#=12\catcode`\_=12\catcode`\%=12\catcode`\&=12\catcode`\$=12\catcode`\~=12\relax} + +% Style switches and the beginning/end of an URL +\let\urlprefix\it +\let\urlsuffix\/ + +% Kern to place between "//" in an URL +\newdimen\urlinterslashkern +\urlinterslashkern=-0.1em + +% Internal macro for typesetting of URLs +\def\displayurl#1:#2#3^^X{{\urlprefix #1:#2\ifx#2/\kern\urlinterslashkern\fi#3\urlsuffix}} -- 2.39.2