Skip to content

Commit

Permalink
feature: parameterize the language in the configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
lthms committed Jul 14, 2017
1 parent 689a502 commit 65fbda8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/Main.hs
Expand Up @@ -34,7 +34,7 @@ main = do


f <- getNovelStructure $ conf f <- getNovelStructure $ conf


case f of Just x -> do res <- stringify (novelify x) case f of Just x -> do res <- stringify (language x) (novelify x)
T.hPutStr h res T.hPutStr h res
Nothing -> T.hPutStrLn stderr "error while parsing" Nothing -> T.hPutStrLn stderr "error while parsing"


Expand Down
15 changes: 10 additions & 5 deletions src/Celtchar/Novel.hs
Expand Up @@ -8,6 +8,7 @@
module Celtchar.Novel where module Celtchar.Novel where


import Control.Monad.State.Strict import Control.Monad.State.Strict
import Control.Monad.Reader
import Data.Default import Data.Default
import Data.String import Data.String
import Data.Maybe import Data.Maybe
Expand All @@ -24,7 +25,10 @@ import Celtchar.Metadata
import Celtchar.Novel.Ogmarkup import Celtchar.Novel.Ogmarkup
import Celtchar.Novel.Structure import Celtchar.Novel.Structure


type Builder = StateT Text IO type Builder = StateT Text (ReaderT Language IO)

getLanguage :: Builder Language
getLanguage = lift $ ask


append :: Text -> Builder () append :: Text -> Builder ()
append str = do st <- get append str = do st <- get
Expand All @@ -34,8 +38,8 @@ appendLn :: Text -> Builder ()
appendLn str = do append str appendLn str = do append str
append "\n" append "\n"


stringify :: Builder () -> IO Text stringify :: Language -> Builder () -> IO Text
stringify builder = execStateT builder "" stringify lang builder = runReaderT (execStateT builder "") lang


class Novelify a where class Novelify a where
novelify :: a -> Builder () novelify :: a -> Builder ()
Expand All @@ -47,10 +51,11 @@ instance (Novelify a) => Novelify [a] where


instance Novelify Document where instance Novelify Document where
novelify (Document path) = do novelify (Document path) = do
lang <- getLanguage
f <- liftIO $ T.readFile path f <- liftIO $ T.readFile path
case parseMetadata path f of case parseMetadata path f of
Right (metadata :: Maybe Text, txt) -> appendLn $ case takeExtension path of Right (metadata :: Maybe Text, txt) -> appendLn $ case takeExtension path of
".up" -> parseDoc txt ".up" -> parseDoc lang txt
".tex" -> txt ".tex" -> txt
".md" -> parseMd f txt ".md" -> parseMd f txt
_ -> verbatim txt _ -> verbatim txt
Expand Down Expand Up @@ -78,7 +83,7 @@ instance Novelify Manuscript where
instance Novelify Novel where instance Novelify Novel where
novelify n = do novelify n = do
appendLn [st|\documentclass[b5paper,12pt]{memoir} appendLn [st|\documentclass[b5paper,12pt]{memoir}
\usepackage[french]{babel} \usepackage[#{show $ language n}]{babel}
\usepackage[T1]{fontenc} \usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
\usepackage[urw-garamond]{mathdesign} \usepackage[urw-garamond]{mathdesign}
Expand Down
7 changes: 5 additions & 2 deletions src/Celtchar/Novel/Ogmarkup.hs
Expand Up @@ -11,7 +11,9 @@ import Data.Text (Text, append)
import Data.String import Data.String
import Text.Shakespeare.Text import Text.Shakespeare.Text


data NovConf = NovConf import Celtchar.Novel.Structure (Language(..))

data NovConf = NovConf Language


el :: Text el :: Text
el = "\n\n" el = "\n\n"
Expand All @@ -21,7 +23,8 @@ blk :: Text
blk = (`append` el) blk = (`append` el)


instance GenConf NovConf Text where instance GenConf NovConf Text where
typography _ = frenchTypo typography (NovConf French) = frenchTypo
typography (NovConf English) = englishTypo


printSpace _ None = "" printSpace _ None = ""
printSpace _ Normal = " " printSpace _ Normal = " "
Expand Down
17 changes: 16 additions & 1 deletion src/Celtchar/Novel/Structure.hs
Expand Up @@ -8,6 +8,9 @@ module Celtchar.Novel.Structure where
import Data.Yaml import Data.Yaml
import GHC.Generics import GHC.Generics


data Language = French | English
deriving (Generic)

data Document = Document FilePath data Document = Document FilePath
deriving (Generic, Show) deriving (Generic, Show)


Expand Down Expand Up @@ -36,12 +39,24 @@ data Manuscript = Manuscript [Part]
instance FromJSON Manuscript where instance FromJSON Manuscript where
parseJSON v = Manuscript <$> parseJSON v parseJSON v = Manuscript <$> parseJSON v


data Novel = Novel { author :: String instance FromJSON Language where
parseJSON (String "english") = pure English
parseJSON (String "french") = pure French
parseJSON _ = fail "unknown language"

instance Show Language where
show English = "english"
show French = "french"

data Novel = Novel { author :: String
, language :: Language
, novelTitle :: String , novelTitle :: String
, manuscript :: Manuscript } , manuscript :: Manuscript }
deriving (Generic, Show) deriving (Generic, Show)

instance FromJSON Novel where instance FromJSON Novel where
parseJSON (Object v) = Novel <$> v .: "author" parseJSON (Object v) = Novel <$> v .: "author"
<*> v .: "language"
<*> v .: "title" <*> v .: "title"
<*> v .: "manuscript" <*> v .: "manuscript"


Expand Down

0 comments on commit 65fbda8

Please sign in to comment.