small fixes to smart-quotes and wrap-hanging-quotes

pull/27/head
Matthew Butterick 11 years ago
parent 25659e88dc
commit ef9d152207

@ -121,9 +121,9 @@
(define quotes (define quotes
'((#px"(?<=\\w)'(?=\\w)" "") ; apostrophe '((#px"(?<=\\w)'(?=\\w)" "") ; apostrophe
(#px"(?<!\\w)'(?=\\w)" "") ; single_at_beginning (#px"(?<!\\w)'(?=\\S)" "") ; single_at_beginning
(#px"(?<=\\S)'(?!\\w)" "") ; single_at_end (#px"(?<=\\S)'(?!\\w)" "") ; single_at_end
(#px"(?<!\\w)\"(?=\\w)" "") ; double_at_beginning (#px"(?<!\\w)\"(?=\\S)" "") ; double_at_beginning
(#px"(?<=\\S)\"(?!\\w)" ""))) ; double_at_end (#px"(?<=\\S)\"(?!\\w)" ""))) ; double_at_end
((make-replacer quotes) str)) ((make-replacer quotes) str))
@ -176,8 +176,8 @@
; todo: improve this ; todo: improve this
; does not handle <p>“<em>thing</em> properly ; does not handle <p>“<em>thing</em> properly
(define+provide/contract (wrap-hanging-quotes nx (define+provide/contract (wrap-hanging-quotes nx
#:single-prepend [single-pp 'squo] #:single-prepend [single-pp '(squo)]
#:double-prepend [double-pp 'dquo]) #:double-prepend [double-pp '(dquo)])
((txexpr?) (#:single-prepend list? #:double-prepend list?) . ->* . txexpr?) ((txexpr?) (#:single-prepend list? #:double-prepend list?) . ->* . txexpr?)
(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))))
@ -192,9 +192,9 @@
[(str-first . in? . '("\"" "")) [(str-first . in? . '("\"" ""))
;; can wrap with any inline tag ;; can wrap with any inline tag
;; so that linebreak detection etc still works ;; so that linebreak detection etc still works
`(,double-pp ,(->string #\“) ,str-rest)] `(,@double-pp ,(->string #\“) ,str-rest)]
[(str-first . in? . '("\'" "")) [(str-first . in? . '("\'" ""))
`(,single-pp ,(->string #\) ,str-rest)] `(,@single-pp ,(->string #\) ,str-rest)]
[else tcs])] [else tcs])]
[(? txexpr? nx) (wrap-hanging-quotes nx)] [(? txexpr? nx) (wrap-hanging-quotes nx)]
[else (car elements)])]) [else (car elements)])])

@ -6,7 +6,7 @@
(check-equal? (smart-dashes "I had --- maybe 13 -- 20 --- hob-nobs.") "I had—maybe 1320—hob-nobs.") (check-equal? (smart-dashes "I had --- maybe 13 -- 20 --- hob-nobs.") "I had—maybe 1320—hob-nobs.")
(check-equal? (smart-quotes "\"Why,\" she could've asked, \"are we in Oahu watching 'Mame'?\"") (check-equal? (smart-quotes "\"Why,\" she could've asked, \"are we in Oahu watching 'Mame'?\"")
"“Why,” she couldve asked, “are we in Oahu watching Mame?”") "“Why,” she couldve asked, “are we in Oahu watching Mame?”")
(check-equal? (smart-quotes "\"\'Impossible.\' Yes.\"") "Impossible. Yes.”")
;; todo: make some tougher tests, it gets flaky with edge cases ;; todo: make some tougher tests, it gets flaky with edge cases
@ -23,6 +23,7 @@
(check-equal? (wrap-hanging-quotes '(p "'Hi' there")) '(p (squo "" "Hi' there"))) (check-equal? (wrap-hanging-quotes '(p "'Hi' there")) '(p (squo "" "Hi' there")))
(check-equal? (wrap-hanging-quotes '(p "'Hi' there") #:single-prepend '(foo ((bar "ino")))) (check-equal? (wrap-hanging-quotes '(p "'Hi' there") #:single-prepend '(foo ((bar "ino"))))
'(p (foo ((bar "ino")) "" "Hi' there"))) '(p (foo ((bar "ino")) "" "Hi' there")))
;; make sure txexpr without elements passes through unscathed ;; make sure txexpr without elements passes through unscathed
(check-equal? (wrap-hanging-quotes '(div ((style "height:2em")))) '(div ((style "height:2em")))) (check-equal? (wrap-hanging-quotes '(div ((style "height:2em")))) '(div ((style "height:2em"))))

Loading…
Cancel
Save