From 4c2a7c4f0ce420de71fe11a16356552674b11130 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 23 Feb 2014 13:08:27 -0800 Subject: [PATCH] add #:omit-tags parameter --- hyphenate/main.rkt | 37 ++++++++++++++++----------- hyphenate/scribblings/hyphenate.scrbl | 20 ++++++++++----- hyphenate/tests.rkt | 10 ++++++++ 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/hyphenate/main.rkt b/hyphenate/main.rkt index 7ca689bb..168bca34 100644 --- a/hyphenate/main.rkt +++ b/hyphenate/main.rkt @@ -151,21 +151,24 @@ (if (char? joiner) (format "~a" joiner) joiner)) ;; helper macro that applies proc to all strings found in xexpr input -(define-syntax (apply-xexpr-strings stx) - (syntax-case stx () - [(_ proc val) #'(let loop ([x val]) - (cond - [(string? x) (proc x)] - [(txexpr? x) (map-elements loop x)] - [else x]))])) +(define (apply-xexpr-strings proc x [tags-to-omit '()]) +; ((procedure? txexpr?) ((or/c null (listof txexpr-tag?))) . ->* . txexpr?) + (let loop ([x x]) + (cond + [(string? x) (proc x)] + [(and (txexpr? x) (not (member (car x) tags-to-omit))) (cons (car x) (map loop (cdr x)))] + [else x]))) + ;; Hyphenate using a filter procedure. (define+provide+safe (hyphenatef x proc [joiner default-joiner] - #:exceptions [extra-exceptions '()] - #:min-length [min-length default-min-length]) + #:exceptions [extra-exceptions '()] + #:min-length [min-length default-min-length] + #:omit-tags [tags-to-omit '()]) ((xexpr? procedure?) ((or/c char? string?) #:exceptions (listof exception-word?) - #:min-length (or/c integer? #f)) . ->* . xexpr/c) + #:min-length (or/c integer? #f) + #:omit-tags (or/c null (listof txexpr-tag?))) . ->* . xexpr/c) ;; set up module data ;; todo?: change set! to parameterize @@ -178,17 +181,21 @@ (define (insert-hyphens text) (regexp-replace* word-pattern text (λ(word) (if (proc word) (string-join (word->hyphenation-points word min-length) joiner-string) word)))) - (apply-xexpr-strings insert-hyphens x)) + (apply-xexpr-strings insert-hyphens x tags-to-omit)) ;; Default hyphenate is a special case of hyphenatef. (define+provide+safe (hyphenate x [joiner default-joiner] - #:exceptions [extra-exceptions '()] - #:min-length [min-length default-min-length]) + #:exceptions [extra-exceptions '()] + #:min-length [min-length default-min-length] + #:omit-tags [tags-to-omit '()]) ((xexpr/c) ((or/c char? string?) #:exceptions (listof exception-word?) - #:min-length (or/c integer? #f)) . ->* . xexpr/c) - (hyphenatef x (λ(x) #t) joiner #:exceptions extra-exceptions #:min-length min-length)) + #:min-length (or/c integer? #f) + #:omit-tags (or/c null (listof txexpr-tag?))) . ->* . xexpr/c) + (hyphenatef x (λ(x) #t) joiner #:exceptions extra-exceptions #:min-length min-length #:omit-tags tags-to-omit)) + + ;; Remove hyphens. diff --git a/hyphenate/scribblings/hyphenate.scrbl b/hyphenate/scribblings/hyphenate.scrbl index ad0b63f3..36c3df3f 100644 --- a/hyphenate/scribblings/hyphenate.scrbl +++ b/hyphenate/scribblings/hyphenate.scrbl @@ -1,9 +1,9 @@ #lang scribble/manual -@(require scribble/eval (for-label racket "../main.rkt" xml)) +@(require scribble/eval (for-label txexpr racket "../main.rkt" xml)) @(define my-eval (make-base-eval)) -@(my-eval `(require hyphenate xml)) +@(my-eval `(require (submod txexpr safe) (submod hyphenate safe) xml)) @title{Hyphenate} @@ -39,7 +39,8 @@ Safe mode enables the function contracts documented below. Use safe mode by impo [xexpr xexpr/c] [joiner (or/c char? string?) (integer->char #x00AD)] [#:exceptions exceptions (listof string?) empty] -[#:min-length length (or/c integer? false?) 5]) +[#:min-length length (or/c integer? false?) 5] +[#:omit-tags tags (or/c null (listof txexpr-tag?)) null]) xexpr/c] Hyphenate @racket[_xexpr] by calculating hyphenation points and inserting @racket[_joiner] at those points. By default, @racket[_joiner] is the soft hyphen (Unicode 00AD = decimal 173). Words shorter than @racket[#:min-length] @racket[_length] will not be hyphenated. To hyphenate words of any length, use @racket[#:min-length] @racket[#f]. @@ -96,14 +97,20 @@ You can send HTML-style X-expressions through @racket[hyphenate]. It will recurs Don't send raw HTML or XML through @racket[hyphenate]. It can't distinguish tags and attributes from textual content, so everything will be hyphenated, thus goofing up your file. But you can easily convert your HTML or XML to an X-expression, hyphenate it, and then convert back. -@margin-note{In HTML, be careful not to include any @code{