module Rakka.Page ( PageName , Page(..) , encodePageName , decodePageName , mkPageURI ) where import Data.ByteString.Base (LazyByteString) import qualified Data.ByteString.Char8 as C8 import Data.Encoding import Data.Encoding.UTF8 import Network.HTTP.Lucu import Network.URI import Subversion.Types import System.Time type PageName = String data Page = Redirection { redirName :: PageName , redirDest :: PageName , redirRevision :: Maybe RevNum , redirLastMod :: CalendarTime } | Entity { pageName :: PageName , pageType :: MIMEType , pageIsTheme :: Bool -- text/css 以外では無意味 , pageIsFeed :: Bool -- text/x-rakka 以外では無意味 , pageIsLocked :: Bool , pageIsBoring :: Bool , pageRevision :: Maybe RevNum , pageLastMod :: CalendarTime , pageSummary :: Maybe String , pageOtherLang :: [(String, PageName)] , pageContent :: LazyByteString } -- UTF-8 に encode してから 0x20 - 0x7E の範圍を除いて URI escape する。 encodePageName :: PageName -> FilePath encodePageName = escapeURIString shouldEscape . C8.unpack . encode UTF8 where shouldEscape :: Char -> Bool shouldEscape c | c >= ' ' && c <= '~' = False | otherwise = True -- URI unescape して UTF-8 から decode する。 decodePageName :: FilePath -> PageName decodePageName = decode UTF8 . C8.pack . unEscapeString mkPageURI :: URI -> PageName -> URI mkPageURI baseURI name | uriPath baseURI == "" = baseURI { uriPath = "/" ++ encoded } | uriPath baseURI == "/" = baseURI { uriPath = "/" ++ encoded } | last (uriPath baseURI) == '/' = baseURI { uriPath = uriPath baseURI ++ encoded } | otherwise = baseURI { uriPath = uriPath baseURI ++ "/" ++ encoded } where encoded = encodePageName name