pull/9/head
Matthew Butterick 11 years ago
parent ecb46bf6af
commit 53fd17aafd

@ -28,8 +28,8 @@
(define dashes (define dashes
;; fix em dashes first, else they'll be mistaken for en dashes ;; fix em dashes first, else they'll be mistaken for en dashes
;; [\\s ] is whitespace + nonbreaking space ;; [\\s ] is whitespace + nonbreaking space
'((#px"[\\s ]*(---|—)[\\s ]*" "") ; em dash '((#px"[\\s ]*(---|—)[\\s ]*" "") ; em dash
(#px"[\\s ]*(--|)[\\s ]*" ""))) ; en dash (#px"[\\s ]*(--|)[\\s ]*" ""))) ; en dash
(define smart-quotes (define smart-quotes
'((#px"(?<=\\w)'(?=\\w)" "") ; apostrophe '((#px"(?<=\\w)'(?=\\w)" "") ; apostrophe
@ -48,11 +48,9 @@
((apply compose1 (reverse replacers)) str))) ((apply compose1 (reverse replacers)) str)))
;; insert nbsp between last two words ;; insert nbsp between last two words
(define (nonbreaking-last-space x (define (nonbreaking-last-space x #:nbsp [nbsp (->string #\u00A0)]
#:nbsp [nbsp (->string #\u00A0)] #:minimum-word-length [minimum-word-length 6])
#:minimum-word-length [minimum-word-length 6])
;; todo: parameterize this, as it will be different for each project ;; todo: parameterize this, as it will be different for each project
(define tags-to-pay-attention-to '(p aside)) ; only apply to paragraphs (define tags-to-pay-attention-to '(p aside)) ; only apply to paragraphs
@ -60,25 +58,27 @@
(define (replace-last-space str) (define (replace-last-space str)
(if (#\space . in? . str) (if (#\space . in? . str)
(let ([reversed-str-list (reverse (string->list str))] (let ([reversed-str-list (reverse (string->list str))]
[reversed-nbsp (reverse (string->list nbsp))]) [reversed-nbsp (reverse (string->list (->string nbsp)))])
(define-values (last-word-chars other-chars) (define-values (last-word-chars other-chars)
(splitf-at reversed-str-list (λ(i) (not (eq? i #\space))))) (splitf-at reversed-str-list (λ(i) (not (eq? i #\space)))))
(list->string (reverse (append last-word-chars
; OK for long words to be on their own line. (define front-chars (if (< (len last-word-chars) minimum-word-length) ; OK for long words to be on their own line
(if (< (len last-word-chars) minimum-word-length) ; first char of other-chars will be the space, so use cdr
; first char of other-chars will be the space, so use cdr (string-append (list->string (reverse (cdr other-chars))) (->string nbsp))
(append reversed-nbsp (cdr other-chars)) (list->string (reverse other-chars))))
other-chars))))) `(,front-chars (span [[pollen "no-hyphens"]] ,(list->string (reverse last-word-chars)))))
str)) (list str)))
(define (find-last-word-space x) ; recursively traverse xexpr (define (find-last-word-space x) ; recursively traverse xexpr
(cond (cond
[(string? x) (replace-last-space x)] [(string? x) (replace-last-space x)] ; todo: this assumes a paragraph only has one string in it.
[(txexpr? x) [(txexpr? x)
(let-values([(tag attr elements) (txexpr->values x)]) (let-values([(tag attr elements) (txexpr->values x)])
(if (> (length elements) 0) ; elements is list of xexprs (if (> (length elements) 0) ; elements is list of xexprs
(let-values ([(all-but-last last) (split-at elements (sub1 (length elements)))]) (let-values ([(all-but-last last) (split-at elements (sub1 (length elements)))])
(make-txexpr tag attr `(,@all-but-last ,(find-last-word-space (car last))))) (define result (find-last-word-space (car last)))
(define result-items (if (txexpr? result) (list result) result)) ; might be txexpr, or list of new elements
(make-txexpr tag attr `(,@all-but-last ,@result-items)))
x))] x))]
[else x])) [else x]))
@ -87,14 +87,12 @@
x)) x))
; wrap initial quotes for hanging punctuation ; wrap initial quotes for hanging punctuation
; todo: improve this ; todo: improve this
; does not handle <p>“<em>thing</em> properly ; does not handle <p>“<em>thing</em> properly
(define (wrap-hanging-quotes nx (define (wrap-hanging-quotes nx
#:single-prepend [single-pp '(squo)] #:single-prepend [single-pp '(squo)]
#:double-prepend [double-pp '(dquo)]) #:double-prepend [double-pp '(dquo)])
(define two-or-more-char-string? (λ(i) (and (string? i) (>= (len i) 2)))) (define two-or-more-char-string? (λ(i) (and (string? i) (>= (len i) 2))))
(define-values (tag attr elements) (txexpr->values nx)) (define-values (tag attr elements) (txexpr->values nx))

@ -33,6 +33,7 @@
(parameterize ([world:current-module-root (world:current-module-root)] (parameterize ([world:current-module-root (world:current-module-root)]
[world:current-server-extras-path (world:current-server-extras-path)] [world:current-server-extras-path (world:current-server-extras-path)]
[error-print-width 1000]
[current-cache (make-cache)]) [current-cache (make-cache)])
(serve/servlet pollen-servlet (serve/servlet pollen-servlet
#:port world:server-port #:port world:server-port

Loading…
Cancel
Save