From 326927215c8481f5b9024e1ec9fe121c2bff7d0f Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 28 Jul 2019 14:36:22 +0800 Subject: [PATCH] [eguile-html-utilities] dedupe, fix html * prefer srfi-13 over regex * instead of use * reuse functions defined in eguile-gnc and eguile-utilities * compact functions * move make-regexp toplevel to ensure one compilation --- .../report-system/eguile-html-utilities.scm | 77 +++++++++---------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/gnucash/report/report-system/eguile-html-utilities.scm b/gnucash/report/report-system/eguile-html-utilities.scm index 8a7030bab1..d123f6109a 100644 --- a/gnucash/report/report-system/eguile-html-utilities.scm +++ b/gnucash/report/report-system/eguile-html-utilities.scm @@ -32,29 +32,24 @@ (gnc:module-load "gnucash/report/report-system" 0) (gnc:module-load "gnucash/app-utils" 0) +(use-modules (gnucash report eguile-gnc)) (use-modules (ice-9 regex)) ; for regular expressions (use-modules (srfi srfi-13)) ; for extra string functions -(define-public (escape-html s1) - ;; Convert string s1 to escape HTML special characters < > and & - ;; i.e. convert them to < > and & respectively. - ;; Maybe there's a way to do this in one go... (but order is important) - (set! s1 (regexp-substitute/global #f "&" s1 'pre "&" 'post)) - (set! s1 (regexp-substitute/global #f "<" s1 'pre "<" 'post)) - (regexp-substitute/global #f ">" s1 'pre ">" 'post)) +(define (string-repeat s n) + ;; return a string made of n copies of string s + (string-join (make-list n s) "")) (define-public (nl->br str) ;; Replace newlines with
- (regexp-substitute/global #f "\n" str 'pre "
" 'post)) + (string-substitute-alist str '((#\newline . "
")))) (define-public (nbsp str) ;; Replace spaces with   (non-breaking spaces) - ;; (yes, I know
is non-standard, but webkit splits e.g. "-£40.00" between - ;; the '-' and the '£' without it.) - (string-append - "" - (regexp-substitute/global #f " " str 'pre " " 'post) - "")) + (string-append + "" + (string-substitute-alist str '((#\space . " "))) + "")) (define-public (empty-cells n) ;; Display n empty table cells @@ -63,7 +58,8 @@ (define-public (indent-cells n) ;; Display n empty table cells with width attribute for indenting ;; (the  s are just there in case CSS isn't working) - (display (string-repeat "  " n))) + (display + (string-repeat "  " n))) (define-public (negstyle item) ;; apply styling for negative amounts @@ -86,9 +82,13 @@ (define-public (display-comm-coll-total comm-coll negative?) ;; Display the total(s) of a commodity collector as HTML (for-each - (lambda (pair) - (display (nbsp (gnc:monetary->string pair)))) - (comm-coll 'format gnc:make-gnc-monetary negative?))) + (lambda (pair) + (display (nbsp (gnc:monetary->string pair)))) + (comm-coll 'format gnc:make-gnc-monetary negative?))) + +;; (thanks to Peter Brett for this regexp and the use of match:prefix) +(define fontre + (make-regexp "([[:space:]]+(bold|semi-bold|book|regular|medium|light))?([[:space:]]+(normal|roman|italic|oblique))?([[:space:]]+(condensed))?[[:space:]]+([[:digit:]]+)" regexp/icase)) (define-public (font-name-to-style-info font-name) ;;; Convert a font name as return by a font option to CSS format. @@ -98,31 +98,28 @@ (font-weight "normal") (font-style "normal") (font-size "medium") - (match "") - ; (thanks to Peter Brett for this regexp and the use of match:prefix) - (fontre (make-regexp "([[:space:]]+(bold|semi-bold|book|regular|medium|light))?([[:space:]]+(normal|roman|italic|oblique))?([[:space:]]+(condensed))?[[:space:]]+([[:digit:]]+)" regexp/icase)) (match (regexp-exec fontre font-name))) - (if match - (begin - ; font name parsed OK -- assemble the bits for CSS - (set! font-family (match:prefix match)) - (if (match:substring match 2) - ; weight given -- some need translating + (when match + ;; font name parsed OK -- assemble the bits for CSS + (set! font-family (match:prefix match)) + (if (match:substring match 2) + ;; weight given -- some need translating (let ((weight (match:substring match 2))) (cond - ((string-ci=? weight "bold") (set! font-weight "bold")) - ((string-ci=? weight "semi-bold") (set! font-weight "600")) - ((string-ci=? weight "light") (set! font-weight "200"))))) - (if (match:substring match 4) - ; style + ((string-ci=? weight "bold") (set! font-weight "bold")) + ((string-ci=? weight "semi-bold") (set! font-weight "600")) + ((string-ci=? weight "light") (set! font-weight "200"))))) + (if (match:substring match 4) + ;; style (let ((style (match:substring match 4))) (cond - ((string-ci=? style "italic") (set! font-style "italic")) - ((string-ci=? style "oblique") (set! font-style "oblique"))))) - ; ('condensed' is ignored) - (if (match:substring match 7) - ; size is in points - (set! font-size (string-append (match:substring match 7) "pt"))))) - ; construct the result (the order of these is important) - (string-append "font: " font-weight " " font-style " " font-size " \"" font-family "\";"))) + ((string-ci=? style "italic") (set! font-style "italic")) + ((string-ci=? style "oblique") (set! font-style "oblique"))))) + ;; ('condensed' is ignored) + (if (match:substring match 7) + ;; size is in points + (set! font-size (string-append (match:substring match 7) "pt")))) + ;; construct the result (the order of these is important) + (string-append "font: " font-weight " " font-style + " " font-size " \"" font-family "\";")))