]> mj.ucw.cz Git - ucwmac.git/blobdiff - ucw-link.tex
Nicer warning messages
[ucwmac.git] / ucw-link.tex
index cbe4ec918e5a4e565d2432ce17b587832d4e1366..58a1c67c9253c91e55406728a956611119cabaf5 100644 (file)
@@ -2,9 +2,15 @@
 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
 % -------------------------------------------------------------------------
 
+\ucwdefmodule{link}
+
 % Should clickable links be produced?
 \newif\ifclickable
-\clickabletrue
+\ifpdf
+       \clickabletrue
+\else
+       \clickablefalse
+\fi
 
 % Common style of all clickable links
 \pdflinkmargin=1pt
 \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}}