improvements

main
Matthew Butterick 11 years ago
parent d2f754714f
commit bc483a027e

@ -1,56 +0,0 @@
#lang racket/base
(require "main.rkt")
(require xml)
(require tagged-xexpr)
#|
The following grammar describes expressions that create X-expressions:
xexpr = string
| (list symbol (list (list symbol string) ...) xexpr ...)
| (cons symbol (list xexpr ...))
| symbol
| valid-char?
| cdata
| misc
|#
;; recursively hyphenate strings within xexpr
;; todo: add exclusion #:only [only-proc (λ(x) x)]
(define (hx x)
(cond
[(string? x) (hyphenate x)]
[(tagged-xexpr? x) x]
;;
;;
[else x] ;; catches symbols, valid-chars, and cdata
)
)
#|
(define exclusions '(style script)) ; omit these from ever being hyphenated
(cond
; todo: the only-proc semantics are illogical.
; main issue: keep it out of tags like <style> that parse as textual elements, but are not.
; So two choices, opt-out or opt-in.
; Problem with opt-out: is set of outlier tags like <style> well-defined?
; Won't it make hyphenation naturally overinclusive?
; Problem with opt-in: conceals a lot of tags that naturally live inside other tags
; only reaches text at the "root level" of the tag.
[(tagged-xexpr? x) (if (and (only-proc x) (not ((car x) . in? . exclusions)))
(map-xexpr-elements hyphenate x)
(map-xexpr-elements (λ(x) (if (tagged-xexpr? x) (hyphenate x) x)) x))] ; only process subxexprs
[(string? x) (hyphenate-string x)]
[else x])
|#
;; how to make cdata:
;; (make-cdata #f #f "<![CDATA[foobar]]>")

@ -1,6 +1,6 @@
#lang racket/base
(require racket/string racket/list racket/contract racket/vector)
(require "patterns.rkt" "exceptions.rkt")
(require "patterns.rkt" "exceptions.rkt" tagged-xexpr xml)
(module+ test (require rackunit))
@ -16,13 +16,13 @@
(provide (contract-out
[hyphenate
((string?) ((or/c char? string?) #:exceptions (listof exception-word?) #:min-length (or/c integer? #f)) . ->* . string?)])
((xexpr?) ((or/c char? string?) #:exceptions (listof exception-word?) #:min-length (or/c integer? #f)) . ->* . string?)])
(contract-out
[hyphenatef
((string? procedure?) ((or/c char? string?) #:exceptions (listof exception-word?) #:min-length (or/c integer? #f)) . ->* . string?)])
((xexpr? procedure?) ((or/c char? string?) #:exceptions (listof exception-word?) #:min-length (or/c integer? #f)) . ->* . string?)])
(contract-out
[unhyphenate
((string?) ((or/c char? string?)) . ->* . string?)]))
((xexpr?) ((or/c char? string?)) . ->* . string?)]))
;; global data, define now but set! them later (because they're potentially big & slow)
(define exceptions #f)
@ -159,7 +159,7 @@
;; Hyphenate using a filter procedure.
;; Theoretically possible to do this externally,
;; but it would just mean doing the regexp-replace twice.
(define (hyphenatef text proc [joiner default-joiner] #:exceptions [extra-exceptions '()] #:min-length [min-length default-min-length])
(define (hyphenatef x proc [joiner default-joiner] #:exceptions [extra-exceptions '()] #:min-length [min-length default-min-length])
;; set up module data
@ -170,15 +170,30 @@
(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
(regexp-replace* word-pattern text (λ(word) (if (proc word) (string-join (word->hyphenation-points word min-length) joiner-string) word))))
(define (insert-hyphens text)
(regexp-replace* word-pattern text (λ(word) (if (proc word) (string-join (word->hyphenation-points word min-length) joiner-string) word))))
(let &hyphenate ([x x])
(cond
[(string? x) (insert-hyphens x)]
[(tagged-xexpr? x) (map-elements &hyphenate x)]
[else x])))
;; Default hyphenate function.
(define (hyphenate text [joiner default-joiner] #:exceptions [extra-exceptions '()] #:min-length [min-length default-min-length])
(hyphenatef text (λ(x) #t) joiner #:exceptions extra-exceptions #:min-length min-length))
(define (unhyphenate text [joiner default-joiner])
(string-replace text (joiner->string joiner) ""))
(define (hyphenate x [joiner default-joiner] #:exceptions [extra-exceptions '()] #:min-length [min-length default-min-length])
(hyphenatef x (λ(x) #t) joiner #:exceptions extra-exceptions #:min-length min-length))
(define (unhyphenate x [joiner default-joiner])
(define (remove-hyphens text)
(string-replace text (joiner->string joiner) ""))
(let &unhyphenate ([x x])
(cond
[(string? x) (remove-hyphens x)]
[(tagged-xexpr? x) (map-elements &unhyphenate x)]
[else x])))

@ -15,6 +15,7 @@
(check-equal? (unhyphenate "polyfoomorfoophism" "foo") "polymorphism")
(check-equal? (hyphenate "polymorphism" #\* #:exceptions '("polymo-rphism")) "polymo*rphism")
(check-equal? (hyphenate "circular polymorphism squandering") "cir\u00ADcu\u00ADlar poly\u00ADmor\u00ADphism squan\u00ADder\u00ADing")
(check-equal? (hyphenate '(p "circular polymorphism" amp (em "squandering"))) '(p "cir\u00ADcu\u00ADlar poly\u00ADmor\u00ADphism" amp (em "squan\u00ADder\u00ADing")))
(check-equal? (hyphenate "present project") "present project") ; exception words
;; test these last so exceptions have been set up already
(check-equal? (word->hyphenation-points "polymorphism") '("poly" "mor" "phism"))

Loading…
Cancel
Save