change #:min-ends-length to #:min-left-length and #:min-right-length

main
Matthew Butterick 10 years ago
parent 6aa792d89c
commit a599b1fc8b

File diff suppressed because one or more lines are too long

@ -241,6 +241,12 @@ a:hover {
height: 4rem;
}
.nosearchform {
margin: 0;
padding: 0;
height: 4rem;
}
.searchbox {
font-size: 1rem;
width: 12rem;
@ -304,7 +310,20 @@ a:hover {
border-left: 0.4rem solid #ccb;
}
.refcontent p {
/* slightly different handling for margin-note* on narrow screens */
@media all and (max-width:1260px) {
span.refcolumn {
float: right;
width: 50%;
margin-left: 1rem;
margin-bottom: 0.8rem;
margin-top: 1.2rem;
}
}
.refcontent, .refcontent p {
line-height: 1.5;
margin: 0;
}

@ -122,6 +122,10 @@ table td {
padding: 0;
}
.nosearchform {
display: none;
}
.searchbox {
width: 16em;
margin: 0px;

@ -23,7 +23,8 @@
;; module default values
(define default-min-length 5)
(define default-min-ends-length 2)
(define default-min-left-length 2)
(define default-min-right-length 2)
(define default-joiner #\u00AD)
(define (add-pattern-to-cache pat)
@ -127,16 +128,18 @@
;; Find hyphenation points in a word. This is not quite synonymous with syllables.
(define (word->hyphenation-points word [min-length default-min-length] [min-ends-length default-min-ends-length])
(define (word->hyphenation-points word [min-length default-min-length]
[min-left-length default-min-left-length]
[min-right-length default-min-right-length])
(define (add-no-hyphen-zone points)
; points is a list corresponding to the letters of the word.
; to create a no-hyphenation zone of length n, zero out the first n-1 points
; and the last n points (because the last value in points is always superfluous)
(let* ([min-ends-length (or min-ends-length default-min-ends-length)]
[min-ends-length (min min-ends-length (length points))])
(define points-with-zeroes-on-left (append (make-list (sub1 min-ends-length) 0) (drop points (sub1 min-ends-length))))
(define points-with-zeroes-on-left-and-right (append (drop-right points-with-zeroes-on-left min-ends-length) (make-list min-ends-length 0)))
(let* ([min-left-length (min (or min-left-length default-min-left-length) (length points))]
[min-right-length (min (or min-right-length default-min-right-length) (length points))])
(define points-with-zeroes-on-left (append (make-list (sub1 min-left-length) 0) (drop points (sub1 min-left-length))))
(define points-with-zeroes-on-left-and-right (append (drop-right points-with-zeroes-on-left min-right-length) (make-list min-right-length 0)))
points-with-zeroes-on-left-and-right))
(define (make-pieces word)
@ -167,7 +170,8 @@
(define+provide+safe (hyphenate x [joiner default-joiner]
#:exceptions [extra-exceptions '()]
#:min-length [min-length default-min-length]
#:min-ends-length [min-ends-length default-min-ends-length]
#:min-left-length [min-left-length default-min-left-length]
#:min-right-length [min-right-length default-min-right-length]
#:omit-word [omit-word? (λ(x) #f)]
#:omit-string [omit-string? (λ(x) #f)]
#:omit-txexpr [omit-txexpr? (λ(x) #f)])
@ -177,7 +181,8 @@
#:omit-word (string? . -> . any/c)
#:omit-string (string? . -> . any/c)
#:omit-txexpr (txexpr? . -> . any/c)
#:min-ends-length (or/c integer? #f)) . ->* . xexpr/c)
#:min-left-length (or/c (and/c integer? positive?) #f)
#:min-right-length (or/c (and/c integer? positive?) #f)) . ->* . xexpr/c)
(initialize-patterns) ; reset everything each time hyphenate is called
(for-each add-exception extra-exceptions)
@ -187,7 +192,7 @@
(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 min-ends-length) joiner-string)
(string-join (word->hyphenation-points word min-length min-left-length min-right-length) joiner-string)
word))))
(apply-proc insert-hyphens x omit-string? omit-txexpr?))

@ -40,7 +40,8 @@ Safe mode enables the function contracts documented below. Use safe mode by impo
[joiner (or/c char? string?) (integer->char #x00AD)]
[#:exceptions exceptions (listof string?) empty]
[#:min-length length (or/c integer? false?) 5]
[#:min-ends-length ends-length (or/c integer? false?) 2]
[#:min-left-length left-length (or/c (and/c integer? positive?) #f) 2]
[#:min-right-length right-length (or/c (and/c integer? positive?) #f) 2]
[#:omit-word word-test (string? . -> . any/c) (λ(x) #f)]
[#:omit-string string-test (string? . -> . any/c) (λ(x) #f)]
[#:omit-txexpr txexpr-test (txexpr? . -> . any/c) (λ(x) #f)])
@ -56,14 +57,15 @@ Hyphenate @racket[_xexpr] by calculating hyphenation points and inserting @racke
(hyphenate "ergo polymorphism" #:min-length #f)
]
The @racket[#:min-ends-length] keyword argument sets a minimum distance between a potential hyphen and either end of the word. The default is 2 characters. Larger values will reduce hyphens, but also prevent small word breaks. This value will override a smaller @racket[#:min-length] value.
The @racket[#:min-left-length] and @racket[#:min-right-length] keyword arguments set the minimum distance between a potential hyphen and the left or right ends of the word. The default is 2 characters. Larger values will reduce hyphens, but also prevent small words from breaking. These values will override a smaller @racket[#:min-length] value.
@examples[#:eval my-eval
(hyphenate "ergo polymorphism" #\-)
(hyphenate "ergo polymorphism" #\- #:min-ends-length #f)
(hyphenate "ergo polymorphism" #\- #:min-ends-length 5)
(code:comment @#,t{Words won't be hyphenated becase of large #:min-ends-length})
(hyphenate "ergo polymorphism" #\- #:min-length #f #:min-ends-length 15)
(hyphenate "ergo polymorphism" #\- #:min-left-length #f)
(hyphenate "ergo polymorphism" #\- #:min-length 2 #:min-left-length 5)
(hyphenate "ergo polymorphism" #\- #:min-right-length 6)
(code:comment @#,t{Next words won't be hyphenated becase of large #:min-left-length})
(hyphenate "ergo polymorphism" #\- #:min-length #f #:min-left-length 15)
]
Because the hyphenation is based on an algorithm rather than a dictionary, it makes good guesses with unusual words:

@ -59,6 +59,7 @@
(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"))
(check-equal? (hyphenate "polymorphism" #\- #:min-ends-length 5) "polymor-phism")
(check-equal? (hyphenate "polymorphism" #\- #:min-ends-length 7) "polymorphism")
(check-equal? (hyphenate "polymorphism" #\- #:min-left-length 5 #:min-right-length 5) "polymor-phism")
(check-equal? (hyphenate "polymorphism" #\- #:min-left-length 3 #:min-right-length 7) "poly-morphism")
(check-equal? (hyphenate "polymorphism" #\- #:min-left-length 7 #:min-right-length 7) "polymorphism")
(check-equal? (hyphenate "polymorphism" #\* #:exceptions '("polymo-rphism")) "polymo*rphism")
Loading…
Cancel
Save