groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 20/20: tmac/an-ext.tmac: Refactor to reduce code size.


From: G. Branden Robinson
Subject: [groff] 20/20: tmac/an-ext.tmac: Refactor to reduce code size.
Date: Thu, 24 Nov 2022 22:11:37 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit 8ab85a9a4191b9093bf02537fa9fcf7ac2e25cbf
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Thu Nov 24 20:45:21 2022 -0600

    tmac/an-ext.tmac: Refactor to reduce code size.
    
    * tmac/an-ext.tmac: Refactor to reduce code size.  `UE` and `ME` have
      nearly identical implementations so...
    
    (mQ): Define new macro to perform the requisite actions, and...
    
    (UE, ME): Make these into wrappers calling mQ.
    
    This is preparation to move most or all of the output device-sensitive
    logic, which is already groff-specific anyway, out of this file and into
    an.tmac.  I have some reasons for this.
    
    1.  It will leave the implementations of the UR/UE/MT/ME macros for
        non-groff formatters much simpler, less scary, and easier to copy
        and paste.  I hope this will drive their further adoption.
    2.  It will free up some room in the name space m[A-Z] used by our
        man(7) extensions, which is getting cramped.
    3.  The 'pdf' output device uses a different technique to embed
        hyperlinks than is used by the 'html' and terminal output drivers.
        (The `pdfhref` macro wants the link text as an argument, and we have
        it trapped in a diversion; it wants the trailing text right away as
        part of the same macro call; and so on.)  To avoid complex logic
        which will be even longer, harder to maintain, and scarier (see
        point #1), I want to yank all this business into "an.tmac" where we
        have the freedom to throw our elbows with arbitrarily long macro
        names, and then to implement a kind of "dynamic dispatch", with
        specialized macros for dealing with hyperlinks on a per-device basis
        instead of one large, complicated macro.
---
 ChangeLog        | 11 ++++++++
 tmac/an-ext.tmac | 84 +++++++++++++++++++++++---------------------------------
 2 files changed, 45 insertions(+), 50 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index aec17d7eb..ab06a1b67 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2022-11-22  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       tmac/an-ext.tmac: Refactor to reduce code size.
+
+       * tmac/an-ext.tmac: Refactor to reduce code size.  `UE` and `ME`
+       have nearly identical implementations so...
+
+       (mQ): Define new macro to perform the requisite actions, and...
+
+       (UE, ME): Make these into wrappers calling mQ.
+
 2022-11-22  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        [man]: Add unit tests for `MT/ME` and `UR/UE`.
diff --git a/tmac/an-ext.tmac b/tmac/an-ext.tmac
index e2f5c48f9..b8c5ddbbb 100644
--- a/tmac/an-ext.tmac
+++ b/tmac/an-ext.tmac
@@ -119,14 +119,17 @@
 .  rr mK
 ..
 .
-.\" Start URL.
-.de UR
-.  mV \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
-..
 .
+.\" Emit hyperlink.  The first argument prefixes a URI scheme and colon
+.\" (e.g., "mailto:";) but since Web URLs generally supply their own,
+.\" it is not used for those (but must be present to keep the argument
+.\" count consistent).  An optional second argument supplies trailing
+.\" punctuation after link text.  `ME` and `UE` call this.
+.de mQ
+.  ds mO mailto:\";
+.  if !'\\$1'\\*(mO' \
+.    ds mO \" empty
 .
-.\" End URL.
-.de UE
 .  ie \\n(mD&\\nU&\\n(mU \{\
 .    br
 .    di
@@ -135,9 +138,9 @@
 .    \" Has there been at least one input line of hyperlinked text?
 .    ie \\n(dn \{\
 .      if \\n(mH \
-\X^html:<a href="\\*(m1">^\c
+\X^html:<a href="\\*(mO\\*(m1">^\c
 .      if \\n(mY \
-\X^tty: link \\*(m1^\c
+\X^tty: link \\*(mO\\*(m1^\c
 .      \" Strip off the final newline of the diversion and emit it.
 .      do chop link-text-div
 .      do link-text-div
@@ -147,23 +150,40 @@
 .      if \\n(mY \
 \X^tty: link^\c
 .    \}
+.    \" If there was no link text, format and link the URI.
 .    el \{\
 .      if \\n(mH \
-\X^html:<a href="\\*(m1">\\*(m1</a>^\c
+\X^html:<a href="\\*(mO\\*(m1">\\*(m1</a>^\c
 .      if \\n(mY \
-\X^tty: link \\*(m1^\\*(m1\X^tty: link^\c
+\X^tty: link \\*(mO\\*(m1^\\*(m1\X^tty: link^\c
 .    \}
+.    do shift
 \&\\$*\"
 .  \}
+.  \" If no link support, format URI in angle brackets.  (The link text
+.  \" was already formatted normally by `mV`.)
 .  el \{\
 .    nh
-\\*(mL\\*(m1\\*(mR\\$1
-.    do shift
+\\*(mL\\*(m1\\*(mR\\$2
+.    do shift 2
 .    ie \n(.g .if \\n(.$ \&\\$*\"
-.    el .if \\n(.$>1 \&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9\"
+.    el .if \\n(.$>2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9\"
 .    hy \\n(mJ
 .  \}
 .  rr mD
+.  rm mO
+..
+.
+.
+.\" Start URL.
+.de UR
+.  mV \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
+..
+.
+.
+.\" End URL.
+.de UE
+.  mQ https: \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
 ..
 .
 .
@@ -175,43 +195,7 @@
 .
 .\" End email address.
 .de ME
-.  ie \\n(mD&\\nU&\\n(mU \{\
-.    br
-.    di
-.    ev
-.
-.    \" Has there been at least one input line of hyperlinked text?
-.    ie \\n(dn \{\
-.      if \\n(mH \
-\X^html:<a href="mailto:\\*(m1">^\c
-.      if \\n(mY \
-\X^tty: link mailto:\\*(m1^\c
-.      \" Strip off the final newline of the diversion and emit it.
-.      do chop link-text-div
-.      do link-text-div
-\c
-.      if \\n(mH \
-\X^html:</a>^\c
-.      if \\n(mY \
-\X^tty: link^\c
-.    \}
-.    el \{\
-.      if \\n(mH \
-\X^html:<a href="mailto:\\*(m1">\\*(m1</a>^\c
-.      if \\n(mY \
-\X^tty: link mailto:\\*(m1^\\*(m1\X^tty: link^\c
-.    \}
-\&\\$*\"
-.  \}
-.  el \{\
-.    nh
-\\*(mL\\*(m1\\*(mR\\$1
-.    do shift
-.    ie \n(.g .if \\n(.$ \&\\$*\"
-.    el .if \\n(.$>1 \&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9\"
-.    hy \\n(mJ
-.  \}
-.  rr mD
+.  mQ mailto: \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
 ..
 .
 .



reply via email to

[Prev in Thread] Current Thread [Next in Thread]