diff --git a/basis/farkup/farkup-tests.factor b/basis/farkup/farkup-tests.factor index aa9345e1d0..ee09486a03 100644 --- a/basis/farkup/farkup-tests.factor +++ b/basis/farkup/farkup-tests.factor @@ -92,22 +92,22 @@ link-no-follow? off [ "

=

foo

" ] [ "===foo==" convert-farkup ] unit-test [ "

foo

=

" ] [ "=foo==" convert-farkup ] unit-test -[ "
int main()\n
" ] +[ "
int main()
" ] [ "[c{int main()}]" convert-farkup ] unit-test -[ "

" ] [ "[[image:lol.jpg]]" convert-farkup ] unit-test -[ "

teh lol

" ] [ "[[image:lol.jpg|teh lol]]" convert-farkup ] unit-test -[ "

http://lol.com

" ] [ "[[http://lol.com]]" convert-farkup ] unit-test -[ "

haha

" ] [ "[[http://lol.com|haha]]" convert-farkup ] unit-test -[ "

Bar

" ] [ "[[Foo/Bar]]" convert-farkup ] unit-test +[ "

" ] [ "[[image:lol.jpg]]" convert-farkup ] unit-test +[ "

\"teh

" ] [ "[[image:lol.jpg|teh lol]]" convert-farkup ] unit-test +[ "

http://lol.com

" ] [ "[[http://lol.com]]" convert-farkup ] unit-test +[ "

haha

" ] [ "[[http://lol.com|haha]]" convert-farkup ] unit-test +[ "

Bar

" ] [ "[[Foo/Bar]]" convert-farkup ] unit-test "/wiki/view/" relative-link-prefix [ - [ "

Bar

" ] [ "[[Foo/Bar]]" convert-farkup ] unit-test + [ "

Bar

" ] [ "[[Foo/Bar]]" convert-farkup ] unit-test ] with-variable [ ] [ "[{}]" convert-farkup drop ] unit-test -[ "
hello\n
" ] [ "[{hello}]" convert-farkup ] unit-test +[ "
hello
" ] [ "[{hello}]" convert-farkup ] unit-test [ "

Feature comparison:\n
aFactorJavaLisp
CoolnessYesNoNo
BadassYesNoNo
EnterpriseYesYesNo
KosherYesNoYes

" @@ -118,15 +118,15 @@ link-no-follow? off ] [ "Feature comparison:\n\n|a|Factor|Java|Lisp|\n|Coolness|Yes|No|No|\n|Badass|Yes|No|No|\n|Enterprise|Yes|Yes|No|\n|Kosher|Yes|No|Yes|\n" convert-farkup ] unit-test [ - "

This wiki is written in Factor and is hosted on a http://linode.com virtual server.

" + "

This wiki is written in Factor and is hosted on a http://linode.com virtual server.

" ] [ "This wiki is written in [[Factor]] and is hosted on a [[http://linode.com|http://linode.com]] virtual server." convert-farkup ] unit-test -[ "

a c

" ] [ "[[a]] [[b|c]]" convert-farkup ] unit-test +[ "

a c

" ] [ "[[a]] [[b|c]]" convert-farkup ] unit-test -[ "

C++

" ] [ "[[C++]]" convert-farkup ] unit-test +[ "

C++

" ] [ "[[C++]]" convert-farkup ] unit-test [ "

<foo>

" ] [ "" convert-farkup ] unit-test @@ -138,10 +138,10 @@ link-no-follow? off [ "
" ] [ "___" convert-farkup ] unit-test [ "
\n" ] [ "___\n" convert-farkup ] unit-test -[ "

before:\n

{ 1 2 3 } 1 tail\n

" ] +[ "

before:\n

{ 1 2 3 } 1 tail

" ] [ "before:\n[factor{{ 1 2 3 } 1 tail}]" convert-farkup ] unit-test -[ "

Factor-rific!

" ] +[ "

Factor-rific!

" ] [ "[[Factor]]-rific!" convert-farkup ] unit-test [ "

[ factor { 1 2 3 }]

" ] @@ -163,7 +163,7 @@ link-no-follow? off convert-farkup string>xml-chunk "a" deep-tag-named "href" swap at url-decode ; -[ "Trader Joe's" ] [ "[[Trader Joe's]]" check-link-escaping ] unit-test +[ "Trader Joe\"s" ] [ "[[Trader Joe\"s]]" check-link-escaping ] unit-test [ "" ] [ "[[]]" check-link-escaping ] unit-test [ "&blah;" ] [ "[[&blah;]]" check-link-escaping ] unit-test -[ "C++" ] [ "[[C++]]" check-link-escaping ] unit-test \ No newline at end of file +[ "C++" ] [ "[[C++]]" check-link-escaping ] unit-test diff --git a/basis/farkup/farkup.factor b/basis/farkup/farkup.factor index 1bfd420dd3..4403d743d6 100644 --- a/basis/farkup/farkup.factor +++ b/basis/farkup/farkup.factor @@ -2,8 +2,9 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays combinators html.elements io io.streams.string kernel math namespaces peg peg.ebnf -sequences sequences.deep strings xml.entities -vectors splitting xmode.code2html urls.encoding ; +sequences sequences.deep strings xml.entities xml.interpolate +vectors splitting xmode.code2html urls.encoding xml.data +xml.writer ; IN: farkup SYMBOL: relative-link-prefix @@ -74,6 +75,7 @@ inline-code = "%" (!("%" | nl).)+ "%" => [[ second >string inline-code boa ]] link-content = (!("|"|"]").)+ + => [[ >string ]] image-link = "[[image:" link-content "|" link-content "]]" => [[ [ second >string ] [ fourth >string ] bi image boa ]] @@ -146,7 +148,7 @@ named-code simple-code = "[{" (!("}]").)+ "}]" - => [[ second f swap code boa ]] + => [[ second >string f swap code boa ]] code = named-code | simple-code @@ -163,66 +165,75 @@ stand-alone { [ dup [ 127 > ] contains? ] [ drop invalid-url ] } { [ dup first "/\\" member? ] [ drop invalid-url ] } { [ CHAR: : over member? ] [ dup absolute-url? [ drop invalid-url ] unless ] } - [ relative-link-prefix get prepend ] - } cond ; + [ relative-link-prefix get prepend "" like ] + } cond url-encode ; -: escape-link ( href text -- href-esc text-esc ) - [ check-url ] dip escape-string ; +: write-link ( href text -- xml ) + [ check-url link-no-follow? get "true" and ] dip + [XML nofollow=<->><-> XML] ; -: write-link ( href text -- ) - escape-link - [ ] - [ write ] - bi* ; - -: write-image-link ( href text -- ) +: write-image-link ( href text -- xml ) disable-images? get [ 2drop - "Images are not allowed" write + [XML Images are not allowed XML] ] [ - escape-link - [ ] bi* + [ check-url ] [ f like ] bi* + [XML alt=<->/> XML] ] if ; -: render-code ( string mode -- string' ) - [ string-lines ] dip - [ -
-            htmlize-lines
-        
- ] with-string-writer write ; +: render-code ( string mode -- xml ) + [ string-lines ] dip htmlize-lines + [XML
<->
XML] ; -GENERIC: (write-farkup) ( farkup -- ) -: ( string -- ) write ; -: ( string -- )
write ; -: in-tag. ( obj quot string -- ) [ call ] keep ; inline -M: heading1 (write-farkup) [ child>> (write-farkup) ] "h1" in-tag. ; -M: heading2 (write-farkup) [ child>> (write-farkup) ] "h2" in-tag. ; -M: heading3 (write-farkup) [ child>> (write-farkup) ] "h3" in-tag. ; -M: heading4 (write-farkup) [ child>> (write-farkup) ] "h4" in-tag. ; -M: strong (write-farkup) [ child>> (write-farkup) ] "strong" in-tag. ; -M: emphasis (write-farkup) [ child>> (write-farkup) ] "em" in-tag. ; -M: superscript (write-farkup) [ child>> (write-farkup) ] "sup" in-tag. ; -M: subscript (write-farkup) [ child>> (write-farkup) ] "sub" in-tag. ; -M: inline-code (write-farkup) [ child>> (write-farkup) ] "code" in-tag. ; -M: list-item (write-farkup) [ child>> (write-farkup) ] "li" in-tag. ; -M: unordered-list (write-farkup) [ child>> (write-farkup) ] "ul" in-tag. ; -M: ordered-list (write-farkup) [ child>> (write-farkup) ] "ol" in-tag. ; -M: paragraph (write-farkup) [ child>> (write-farkup) ] "p" in-tag. ; -M: link (write-farkup) [ href>> ] [ text>> ] bi write-link ; -M: image (write-farkup) [ href>> ] [ text>> ] bi write-image-link ; -M: code (write-farkup) [ string>> ] [ mode>> ] bi render-code ; -M: line (write-farkup) drop
; -M: line-break (write-farkup) drop
nl ; -M: table-row (write-farkup) ( obj -- ) - child>> [ [ [ (write-farkup) ] "td" in-tag. ] each ] "tr" in-tag. ; -M: table (write-farkup) [ child>> (write-farkup) ] "table" in-tag. ; -M: string (write-farkup) escape-string write ; -M: vector (write-farkup) [ (write-farkup) ] each ; -M: f (write-farkup) drop ; +GENERIC: (write-farkup) ( farkup -- xml ) + +: farkup-inside ( farkup name -- xml ) + swap T{ attrs } swap + child>> (write-farkup) 1array ; + +M: heading1 (write-farkup) "h1" farkup-inside ; +M: heading2 (write-farkup) "h2" farkup-inside ; +M: heading3 (write-farkup) "h3" farkup-inside ; +M: heading4 (write-farkup) "h4" farkup-inside ; +M: strong (write-farkup) "strong" farkup-inside ; +M: emphasis (write-farkup) "em" farkup-inside ; +M: superscript (write-farkup) "sup" farkup-inside ; +M: subscript (write-farkup) "sub" farkup-inside ; +M: inline-code (write-farkup) "code" farkup-inside ; +M: list-item (write-farkup) "li" farkup-inside ; +M: unordered-list (write-farkup) "ul" farkup-inside ; +M: ordered-list (write-farkup) "ol" farkup-inside ; +M: paragraph (write-farkup) "p" farkup-inside ; +M: table (write-farkup) "table" farkup-inside ; + +M: link (write-farkup) + [ href>> ] [ text>> ] bi write-link ; + +M: image (write-farkup) + [ href>> ] [ text>> ] bi write-image-link ; + +M: code (write-farkup) + [ string>> ] [ mode>> ] bi render-code ; + +M: line (write-farkup) + drop [XML
XML] ; + +M: line-break (write-farkup) + drop [XML
XML] ; + +M: table-row (write-farkup) + child>> + [ (write-farkup) [XML <-> XML] ] map + [XML <-> XML] ; + +M: string (write-farkup) ; + +M: vector (write-farkup) [ (write-farkup) ] map ; + +M: f (write-farkup) ; : write-farkup ( string -- ) - parse-farkup (write-farkup) ; + parse-farkup (write-farkup) write-xml-chunk ; : convert-farkup ( string -- string' ) - parse-farkup [ (write-farkup) ] with-string-writer ; + [ write-farkup ] with-string-writer ; diff --git a/basis/xmode/code2html/code2html.factor b/basis/xmode/code2html/code2html.factor index 032b2b25f0..4cdef4043e 100644 --- a/basis/xmode/code2html/code2html.factor +++ b/basis/xmode/code2html/code2html.factor @@ -1,48 +1,45 @@ -USING: xmode.tokens xmode.marker xmode.catalog kernel +USING: xmode.tokens xmode.marker xmode.catalog kernel locals html.elements io io.files sequences words io.encodings.utf8 -namespaces xml.entities accessors ; +namespaces xml.entities accessors xml.interpolate locals xml.writer ; IN: xmode.code2html -: htmlize-tokens ( tokens -- ) +: htmlize-tokens ( tokens -- xml ) [ [ str>> ] [ id>> ] bi [ - > =class span> escape-string write - ] [ - escape-string write - ] if* - ] each ; + name>> swap + [XML ><-> XML] + ] [ ] if* + ] map ; -: htmlize-line ( line-context line rules -- line-context' ) +: htmlize-line ( line-context line rules -- line-context' xml ) tokenize-line htmlize-tokens ; -: htmlize-lines ( lines mode -- ) - f swap load-mode [ htmlize-line nl ] curry reduce drop ; +: htmlize-lines ( lines mode -- xml ) + f -rot load-mode [ htmlize-line ] curry map nip ; -: default-stylesheet ( -- ) - ; +: default-stylesheet ( -- xml ) + "resource:basis/xmode/code2html/stylesheet.css" + utf8 file-contents + [XML XML] ; -: htmlize-stream ( path stream -- ) - lines swap - +:: htmlize-stream ( path stream -- xml ) + stream lines + [ "" ] [ first find-mode path swap htmlize-lines ] + if-empty :> input + default-stylesheet :> stylesheet + - default-stylesheet - dup escape-string write + <-stylesheet-> + <-path-> -
-                over empty?
-                [ 2drop ]
-                [ over first find-mode htmlize-lines ] if
-            
+
<-input->
- ; + XML> ; : htmlize-file ( path -- ) dup utf8 [ dup ".html" append utf8 [ - input-stream get htmlize-stream + input-stream get htmlize-stream write-xml ] with-file-writer ] with-file-reader ;