]> gitweb.factorcode.org Git - factor.git/commitdiff
Some improvements to farkup link handling
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 7 Sep 2008 23:06:20 +0000 (18:06 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 7 Sep 2008 23:06:20 +0000 (18:06 -0500)
basis/farkup/farkup-tests.factor
basis/farkup/farkup.factor
basis/html/elements/elements.factor

index 0f969347989213744134b13d01d1184bd5384108..0280c1a08d908578e93736f05a46d0e971da335a 100644 (file)
@@ -1,8 +1,11 @@
 ! Copyright (C) 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: farkup kernel peg peg.ebnf tools.test ;
+USING: farkup kernel peg peg.ebnf tools.test namespaces ;
 IN: farkup.tests
 
+[ "Baz" ] [ "Foo/Bar/Baz" simple-link-title ] unit-test
+[ "Baz" ] [ "Baz" simple-link-title ] unit-test
+
 [ ] [
     "abcd-*strong*\nasdifj\nweouh23ouh23"
     "paragraph" \ farkup rule parse drop
@@ -81,10 +84,15 @@ IN: farkup.tests
 [ "<pre><span class='KEYWORD3'>int</span> <span class='FUNCTION'>main</span><span class='OPERATOR'>(</span><span class='OPERATOR'>)</span>\n</pre>" ]
 [ "[c{int main()}]" convert-farkup ] unit-test
 
-[ "<p><img src=\"lol.jpg\"/></p>" ] [ "[[image:lol.jpg]]" convert-farkup ] unit-test
-[ "<p><img src=\"lol.jpg\" alt=\"teh lol\"/></p>" ] [ "[[image:lol.jpg|teh lol]]" convert-farkup ] unit-test
-[ "<p><a href=\"lol.com\">lol.com</a></p>" ] [ "[[lol.com]]" convert-farkup ] unit-test
-[ "<p><a href=\"lol.com\">haha</a></p>" ] [ "[[lol.com|haha]]" convert-farkup ] unit-test
+[ "<p><img src='lol.jpg'/></p>" ] [ "[[image:lol.jpg]]" convert-farkup ] unit-test
+[ "<p><img src='lol.jpg' alt='teh lol'/></p>" ] [ "[[image:lol.jpg|teh lol]]" convert-farkup ] unit-test
+[ "<p><a href='http://lol.com'>http://lol.com</a></p>" ] [ "[[http://lol.com]]" convert-farkup ] unit-test
+[ "<p><a href='http://lol.com'>haha</a></p>" ] [ "[[http://lol.com|haha]]" convert-farkup ] unit-test
+[ "<p><a href='Foo/Bar'>Bar</a></p>" ] [ "[[Foo/Bar]]" convert-farkup ] unit-test
+
+"/wiki/view/" relative-link-prefix [
+    [ "<p><a href='/wiki/view/Foo/Bar'>Bar</a></p>" ] [ "[[Foo/Bar]]" convert-farkup ] unit-test
+] with-variable
 
 [ ] [ "[{}]" convert-farkup drop ] unit-test
 
index c029423714ad292f8e3da8fcb28dee58620a8834..7005232517c65ade3e6192790d916dabdc8c2503 100644 (file)
@@ -28,6 +28,12 @@ TUPLE: link href text ;
 TUPLE: image href text ;
 TUPLE: code mode string ;
 
+: absolute-url? ( string -- ? )
+    { "http://" "https://" "ftp://" } [ head? ] with contains? ;
+
+: simple-link-title ( string -- string' )
+    dup absolute-url? [ "/" last-split1 swap or ] unless ;
+
 EBNF: farkup
 nl               = ("\r\n" | "\r" | "\n") => [[ drop "\n" ]]
 2nl              = nl nl
@@ -67,7 +73,7 @@ image-link       = "[[image:" (!("|") .)+  "|" (!("]]").)+ "]]"
                     => [[ second >string f image boa ]]
 
 simple-link      = "[[" (!("|]" | "]]") .)+ "]]"
-    => [[ second >string dup link boa ]]
+    => [[ second >string dup simple-link-title link boa ]]
 
 labelled-link    = "[[" (!("|") .)+ "|" (!("]]").)+ "]]"
     => [[ [ second >string ] [ fourth >string ] bi link boa ]]
@@ -119,31 +125,26 @@ stand-alone
         { [ dup empty? ] [ drop invalid-url ] }
         { [ dup [ 127 > ] contains? ] [ drop invalid-url ] }
         { [ dup first "/\\" member? ] [ drop invalid-url ] }
-        { [ CHAR: : over member? ] [
-            dup { "http://" "https://" "ftp://" } [ head? ] with contains?
-            [ drop invalid-url ] unless
-        ] }
+        { [ CHAR: : over member? ] [ dup absolute-url? [ drop invalid-url ] unless ] }
         [ relative-link-prefix get prepend ]
     } cond ;
 
 : escape-link ( href text -- href-esc text-esc )
     >r check-url escape-quoted-string r> escape-string ;
 
-: write-link ( text href -- )
+: write-link ( href text -- )
     escape-link
-    "<a" write
-    " href=\"" write write "\"" write
-    link-no-follow? get [ " nofollow=\"true\"" write ] when
-    ">" write write "</a>" write ;
+    [ <a =href link-no-follow? get [ "true" =nofollow ] when a> ]
+    [ write </a> ]
+    bi* ;
 
 : write-image-link ( href text -- )
     disable-images? get [
-        2drop "<strong>Images are not allowed</strong>" write
+        2drop
+        <strong> "Images are not allowed" write </strong>
     ] [
         escape-link
-        >r "<img src=\"" write write "\"" write r>
-        [ " alt=\"" write write "\"" write ] unless-empty
-        "/>" write
+        [ <img =src ] [ [ =alt ] unless-empty img/> ] bi*
     ] if ;
 
 : render-code ( string mode -- string' )
@@ -170,7 +171,7 @@ M: inline-code write-farkup ( obj -- ) [ obj>> write-farkup ] "code" in-tag. ;
 M: list-item write-farkup ( obj -- ) [ obj>> write-farkup ] "li" in-tag. ;
 M: list write-farkup ( obj -- ) [ obj>> write-farkup ] "ul" in-tag. ;
 M: paragraph write-farkup ( obj -- ) [ obj>> write-farkup ] "p" in-tag. ;
-M: link write-farkup ( obj -- ) [ text>> ] [ href>> ] bi write-link ;
+M: link write-farkup ( obj -- ) [ href>> ] [ text>> ] bi write-link ;
 M: image write-farkup ( obj -- ) [ href>> ] [ text>> ] bi write-image-link ;
 M: code write-farkup ( obj -- ) [ string>> ] [ mode>> ] bi render-code ;
 M: table-row write-farkup ( obj -- )
index 35e01227b5e3ff5effe2309af99b4844513df9a3..89f8b01a1979f75a3d331db0796b33aa0be0b9e2 100644 (file)
@@ -142,6 +142,7 @@ SYMBOL: html
     "ol" "li" "form" "a" "p" "html" "head" "body" "title"
     "b" "i" "ul" "table" "tbody" "tr" "td" "th" "pre" "textarea"
     "script" "div" "span" "select" "option" "style" "input"
+    "strong"
 ] [ define-closed-html-word ] each
 
 ! Define some open HTML tags
@@ -160,6 +161,8 @@ SYMBOL: html
     "src" "language" "colspan" "onchange" "rel"
     "width" "selected" "onsubmit" "xmlns" "lang" "xml:lang"
     "media" "title" "multiple" "checked"
+    "summary" "cellspacing" "align" "scope" "abbr"
+    "nofollow" "alt"
 ] [ define-attribute-word ] each
 
 >>