source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Give -Thtml and -Txhtml the gift of recognising escapes when
@ 2011-05-14 16:28 kristaps
  0 siblings, 0 replies; only message in thread
From: kristaps @ 2011-05-14 16:28 UTC (permalink / raw)
  To: source

Log Message:
-----------
Give -Thtml and -Txhtml the gift of recognising escapes when calculating
widths (e.g., `Bl -tag -width "\s[blahblah]bar").  This has long since
been done for -Tascii but escaped noticed with -T[x]html.

Modified Files:
--------------
    mdocml:
        html.c
        html.h
        mdoc_html.c

Revision Data
-------------
Index: html.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/html.h,v
retrieving revision 1.41
retrieving revision 1.42
diff -Lhtml.h -Lhtml.h -u -p -r1.41 -r1.42
--- html.h
+++ html.h
@@ -155,6 +155,7 @@ void		  bufncat(struct html *, const cha
 void		  bufinit(struct html *);
 
 void		  html_idcat(char *, const char *, int);
+int		  html_strlen(const char *);
 
 __END_DECLS
 
Index: html.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/html.c,v
retrieving revision 1.137
retrieving revision 1.138
diff -Lhtml.c -Lhtml.c -u -p -r1.137 -r1.138
--- html.c
+++ html.c
@@ -31,6 +31,7 @@
 #include <unistd.h>
 
 #include "mandoc.h"
+#include "libmandoc.h"
 #include "out.h"
 #include "html.h"
 #include "main.h"
@@ -294,6 +295,41 @@ print_metaf(struct html *h, enum mandoc_
 			print_otag(h, TAG_I, 0, NULL);
 }
 
+int
+html_strlen(const char *cp)
+{
+	int		 ssz, sz;
+	const char	*seq, *p;
+
+	/*
+	 * Account for escaped sequences within string length
+	 * calculations.  This follows the logic in term_strlen() as we
+	 * must calculate the width of produced strings.
+	 * Assume that characters are always width of "1".  This is
+	 * hacky, but it gets the job done for approximation of widths.
+	 */
+
+	sz = 0;
+	while (NULL != (p = strchr(cp, '\\'))) {
+		sz += (int)(p - cp);
+		++cp;
+		switch (mandoc_escape(&cp, &seq, &ssz)) {
+		case (ESCAPE_ERROR):
+			return(sz);
+		case (ESCAPE_PREDEF):
+			sz++;
+			break;
+		case (ESCAPE_SPECIAL):
+			sz++;
+			break;
+		default:
+			break;
+		}
+	}
+
+	assert(sz >= 0);
+	return(sz + strlen(cp));
+}
 
 static int
 print_encode(struct html *h, const char *p, int norecurse)
Index: mdoc_html.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc_html.c,v
retrieving revision 1.165
retrieving revision 1.166
diff -Lmdoc_html.c -Lmdoc_html.c -u -p -r1.165 -r1.166
--- mdoc_html.c
+++ mdoc_html.c
@@ -288,7 +288,7 @@ a2width(const char *p, struct roffsu *su
 
 	if ( ! a2roffsu(p, su, SCALE_MAX)) {
 		su->unit = SCALE_BU;
-		su->scale = (int)strlen(p);
+		su->scale = html_strlen(p);
 	}
 }
 
@@ -355,7 +355,7 @@ a2offs(const char *p, struct roffsu *su)
 		SCALE_HS_INIT(su, INDENT * 2);
 	else if ( ! a2roffsu(p, su, SCALE_MAX)) {
 		su->unit = SCALE_BU;
-		su->scale = (int)strlen(p);
+		su->scale = html_strlen(p);
 	}
 }
 
@@ -703,7 +703,7 @@ mdoc_nm_pre(MDOC_ARGS)
 {
 	struct htmlpair	 tag;
 	struct roffsu	 su;
-	size_t		 len;
+	int		 len;
 
 	switch (n->type) {
 	case (MDOC_ELEM):
@@ -731,10 +731,10 @@ mdoc_nm_pre(MDOC_ARGS)
 
 	for (len = 0, n = n->child; n; n = n->next)
 		if (MDOC_TEXT == n->type)
-			len += strlen(n->string);
+			len += html_strlen(n->string);
 
 	if (0 == len && m->name)
-		len = strlen(m->name);
+		len = html_strlen(m->name);
 
 	SCALE_HS_INIT(&su, (double)len);
 	bufcat_su(h, "width", &su);
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-05-14 16:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-14 16:28 mdocml: Give -Thtml and -Txhtml the gift of recognising escapes when kristaps

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).