]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - Rakka/Wiki/Interpreter/Image.hs
Resurrection from bitrot
[Rakka.git] / Rakka / Wiki / Interpreter / Image.hs
index 97ffc8e1fafebc738a0818af3820548f417ac882..886fdf512732228f1f746b7481d81bf0987b4c07 100644 (file)
@@ -1,47 +1,64 @@
+{-# LANGUAGE
+    OverloadedStrings
+  , RecordWildCards
+  , UnicodeSyntax
+  , ViewPatterns
+  #-}
 module Rakka.Wiki.Interpreter.Image
     ( interpreters
     )
     where
+import Data.Monoid.Unicode
+import Data.Text (Text)
+import qualified Data.Text as T
+import Network.URI
+import Prelude.Unicode
+import Rakka.Page
+import Rakka.SystemConfig
+import Rakka.Wiki.Interpreter
+import Rakka.Wiki
 
-import           Control.Monad
-import           Data.Maybe
-import           Network.URI
-import           Rakka.Page
-import           Rakka.SystemConfig
-import           Rakka.Wiki.Interpreter
-import           Rakka.Wiki
-
-
-interpreters :: [Interpreter]
+interpreters ∷ [Interpreter]
 interpreters = [ imageInterp
                , imgFrameInterp
                ]
 
+-- <img src="[PageName]"
+--      alt="[Alternative]"   -- 省略可能
+--      link="[PageName]"     -- 省略可能、省略時は画像そのものへのリンク
+--      link="[Absolute URI]" -- 絶對 URI の場合はその URI へのリンク
+--      link="" />            -- 空文字列の場合はリンク無し
 
 -- <a href="..." class="inlineImage ...">
 --   <img src="..." alt="..." />
 -- </a>
-imageInterp :: Interpreter
+imageInterp  Interpreter
 imageInterp
     = InlineCommandInterpreter {
-        iciName      = "img"
+        iciName = "img"
       , iciInterpret
-          = \ ctx (InlineCommand _ attrs inside) ->
-            do BaseURI baseURI <- getSysConf (ctxSysConf ctx)
-
-               let pageName    = case lookup "src" attrs of
-                                   Just x  -> x
-                                   Nothing -> error "\"src\" attribute is missing"
-                   hrefAttr    = ("href", uriToString id (mkPageURI baseURI pageName) "")
+          = \(InterpreterContext {..}) (InlineCommand _ attrs _) →
+            do BaseURI baseURI ← getSysConf ctxSysConf
+               let name        = case lookup "src" attrs of
+                                   Just x  → x
+                                   Nothing → error "\"src\" attribute is missing"
+                   link        = case lookup "link" attrs of
+                                   Just x
+                                       | T.null x  → Nothing
+                                       | isURI' x  → Just x
+                                       | otherwise → Just ∘ T.pack $ uriToString id (mkPageURI baseURI x   ) ""
+                                   Nothing         → Just ∘ T.pack $ uriToString id (mkPageURI baseURI name) ""
                    alt         = lookup "alt" attrs
                    classAttr   = case lookup "float" attrs of
-                                   Nothing      -> ("class", "inlineImage")
-                                   Just "left"  -> ("class", "inlineImage leftFloat")
-                                   Just "right" -> ("class", "inlineImage rightFloat")
-                                   Just others  -> error ("unknown \"float\" attribute: " ++ others)
-                   anchorAttrs = [hrefAttr, classAttr]
-
-               return (Anchor anchorAttrs [Image (Right pageName) alt])
+                                   Nothing      → "inlineImage"
+                                   Just "left"  → "inlineImage leftFloat"
+                                   Just "right" → "inlineImage rightFloat"
+                                   Just others  → error ("unknown \"float\" attribute: " ⊕ T.unpack others)
+                   result      = case link of
+                                   Nothing → Span [("class", classAttr)] [Image (Right name) alt]
+                                   Just x  → Anchor [ ("class", classAttr)
+                                                    , ("href" , x        ) ] [Image (Right name) alt]
+               return result
       }
 
 
@@ -55,30 +72,39 @@ imageInterp
 --     ...
 --   </div>
 -- </div>
-imgFrameInterp :: Interpreter
+imgFrameInterp  Interpreter
 imgFrameInterp
     = BlockCommandInterpreter {
-        bciName      = "imgframe"
+        bciName = "imgframe"
       , bciInterpret
-          = \ ctx (BlockCommand _ attrs inside) ->
-            do BaseURI baseURI <- getSysConf (ctxSysConf ctx)
-
-               let pageName    = case lookup "src" attrs of
-                                   Just x  -> x
-                                   Nothing -> error "\"src\" attribute is missing"
-                   hrefAttr    = ("href", uriToString id (mkPageURI baseURI pageName) "")
+          = \(InterpreterContext {..}) (BlockCommand _ attrs inside) →
+            do BaseURI baseURI ← getSysConf ctxSysConf
+               let name        = case lookup "src" attrs of
+                                   Just x  → x
+                                   Nothing → error "\"src\" attribute is missing"
+                   link        = case lookup "link" attrs of
+                                   Just x
+                                       | T.null x  → Nothing
+                                       | isURI' x  → Just x
+                                       | otherwise → Just ∘ T.pack $ uriToString id (mkPageURI baseURI x   ) ""
+                                   Nothing         → Just ∘ T.pack $ uriToString id (mkPageURI baseURI name) ""
                    classAttr   = case lookup "float" attrs of
-                                   Nothing      -> ("class", "imageFrame")
-                                   Just "left"  -> ("class", "imageFrame leftFloat")
-                                   Just "right" -> ("class", "imageFrame rightFloat")
-                                   Just others  -> error ("unknown \"float\" attribute: " ++ others)
+                                   Nothing      → ("class", "imageFrame")
+                                   Just "left"  → ("class", "imageFrame leftFloat")
+                                   Just "right" → ("class", "imageFrame rightFloat")
+                                   Just others  → error ("unknown \"float\" attribute: " ⊕ T.unpack others)
+                   image       = case link of
+                                   Nothing → Image (Right name) Nothing
+                                   Just x  → Anchor [("href" , x)] [Image (Right name) Nothing]
                
                return (Div [classAttr]
                        [ Block (Div [("class", "imageData")]
-                                        [ Inline (Anchor [hrefAttr]
-                                                  [ Image (Right pageName) Nothing ]) ])
+                                        [ Inline image ])
                        , Block (Div [("class", "imageCaption")]
-                                        [ Block x | x <- inside ])
+                                        [ Block x | x  inside ])
                        ]
                       )
       }
+
+isURI' ∷ Text → Bool
+isURI' = isURI ∘ T.unpack
\ No newline at end of file