From 9f6dde282c66ccf4c4d4c397919c54b3db141c0d Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 23 Feb 2014 18:25:44 -0800 Subject: [PATCH] fix #:omit-word in unhyphenate --- hyphenate/main.rkt | 26 ++++++++++++++------------ hyphenate/tests.rkt | 5 ++++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/hyphenate/main.rkt b/hyphenate/main.rkt index c60f05ed..4ec947ad 100644 --- a/hyphenate/main.rkt +++ b/hyphenate/main.rkt @@ -150,8 +150,7 @@ (define (joiner->string joiner) (if (char? joiner) (format "~a" joiner) joiner)) -;; helper macro that applies proc to all strings found in xexpr input -(define (apply-xexpr-strings proc x [omit-string (λ(x) #f)] [omit-txexpr (λ(x) #f)]) +(define (apply-proc proc x [omit-string (λ(x) #f)] [omit-txexpr (λ(x) #f)]) ; ((procedure? txexpr?) ((or/c null (listof txexpr-tag?))) . ->* . txexpr?) (let loop ([x x]) (cond @@ -160,7 +159,6 @@ [else x]))) -;; Hyphenate using a filter procedure. (define+provide+safe (hyphenate x [joiner default-joiner] #:exceptions [extra-exceptions '()] #:min-length [min-length default-min-length] @@ -180,16 +178,17 @@ (when (not pattern-tree) (set! pattern-tree (make-pattern-tree default-patterns))) (define joiner-string (joiner->string joiner)) - (define word-pattern #px"\\w+") ;; more restrictive than exception-word ;; todo?: connect this regexp pattern to the one used in word? predicate + (define word-pattern #px"\\w+") ;; more restrictive than exception-word (define (insert-hyphens text) - (regexp-replace* word-pattern text (λ(word) (if (not (omit-word? word)) (string-join (word->hyphenation-points word min-length) joiner-string) word)))) + (regexp-replace* word-pattern text (λ(word) (if (not (omit-word? word)) + (string-join (word->hyphenation-points word min-length) joiner-string) + word)))) - (apply-xexpr-strings insert-hyphens x omit-string? omit-txexpr?)) + (apply-proc insert-hyphens x omit-string? omit-txexpr?)) -;; Remove hyphens. (define+provide+safe (unhyphenate x [joiner default-joiner] #:omit-word [omit-word? (λ(x) #f)] #:omit-string [omit-string? (λ(x) #f)] @@ -198,8 +197,11 @@ #:omit-word (string? . -> . any/c) #:omit-string (string? . -> . any/c) #:omit-txexpr (txexpr? . -> . any/c)) . ->* . xexpr/c) - -(define (remove-hyphens text) - (string-replace text (joiner->string joiner) "")) - -(apply-xexpr-strings remove-hyphens x omit-string? omit-txexpr?)) + + (define word-pattern (pregexp (format "[\\w~a]+" joiner))) + (define (remove-hyphens text) + (regexp-replace* word-pattern text (λ(word) (if (not (omit-word? word)) + (string-replace word (joiner->string joiner) "") + word)))) + + (apply-proc remove-hyphens x omit-string? omit-txexpr?)) diff --git a/hyphenate/tests.rkt b/hyphenate/tests.rkt index 1b880ae8..e557b46f 100644 --- a/hyphenate/tests.rkt +++ b/hyphenate/tests.rkt @@ -53,4 +53,7 @@ (check-equal? (unhyphenate '(p (script "tail-feathers") (em "tail-feathers")) #\- #:omit-txexpr (λ(x) (member (car x) '(script)))) - '(p (script "tail-feathers") (em "tailfeathers"))) \ No newline at end of file + '(p (script "tail-feathers") (em "tailfeathers"))) + +(check-equal? (unhyphenate '(p "cir-cu-lar poly-mor-phism" "cir-cu-lar poly-mor-phisms") #\- #:omit-string (λ(x) (regexp-match #rx"s$" x))) + '(p "circular polymorphism" "cir-cu-lar poly-mor-phisms")) \ No newline at end of file