Skip to content

Commit

Permalink
Get started with #27
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadapc committed Oct 2, 2017
1 parent 2bc2d61 commit 2be537a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
2 changes: 2 additions & 0 deletions jats2tex.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ library
Text.JaTex.Template.TemplateInterp
Text.JaTex.Template.TemplateInterp.Helpers
Text.JaTex.Template.Types
Text.JaTex.TemplateWrapper
Text.JaTex.TexWriter
Text.JaTex.Upgrade
Text.JaTex.UpgradeTemplate
Text.JaTex.Util
other-modules:
Paths_jats2tex
Expand Down
26 changes: 21 additions & 5 deletions src/Text/JaTex/Cmd/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Text.JaTex.Cmd.Main

import Data.Maybe
import Data.Monoid
import qualified Data.Text.IO as Text
import qualified Data.Text.IO as Text
import GHC.IO.Encoding
import System.FilePath
import System.IO
Expand All @@ -17,11 +17,13 @@ import Options.Applicative
import System.Win32.Console
#endif
import Text.JaTex
import qualified Text.JaTex.Upgrade as Upgrade
import qualified Text.JaTex.Upgrade as Upgrade
import qualified Text.JaTex.UpgradeTemplate as UpgradeTemplate


data Options
= RunUpgrade
| RunUpgradeTemplate FilePath
| RunVersion
| Options { optsOutputFile :: Maybe FilePath
, optsTemplateFile :: Maybe String
Expand All @@ -43,6 +45,17 @@ options =
command
"upgrade"
(info (pure RunUpgrade) (fullDesc <> progDesc "Upgrade jats2tex"))) <|>
subparser
(metavar "upgrade-template" <>
command
"upgrade-template"
(info
(RunUpgradeTemplate <$>
argument
str
(metavar "INPUT_TEMPLATE_FILE" <>
help "The legacy template to upgrade"))
(fullDesc <> progDesc "Upgrade template to version 1"))) <|>
Options <$>
optional
(strOption
Expand All @@ -61,9 +74,11 @@ options =
optional
(strOption
(short 'e' <> long "input-encoding" <> metavar "INPUT_ENCODING" <>
help (unlines [ "The input file/handle encoding (defaults to latin-1)"
, "Output and FFI with Lua is always with UTF-8"
]))) <*>
help
(unlines
[ "The input file/handle encoding (defaults to latin-1)"
, "Output and FFI with Lua is always with UTF-8"
]))) <*>
argument str (metavar "INPUT_FILE" <> help "XML Input File")

optionsPI :: ParserInfo Options
Expand Down Expand Up @@ -94,6 +109,7 @@ run Options{..} = do
Text.writeFile outputFile result
putStrLn $ "Wrote: " <> outputFile
run RunUpgrade = Upgrade.runUpgrade
run (RunUpgradeTemplate fp) = UpgradeTemplate.run fp
run RunVersion = Upgrade.putVersionInfo

makeSafe :: Handle -> IO ()
Expand Down
4 changes: 2 additions & 2 deletions src/Text/JaTex/Template/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data PreparedTemplateNode m
| PreparedTemplateLua (LuaExprType m)
| PreparedTemplatePlain Text

type ConcreteTemplate = [ConcreteTemplateNode]

data ConcreteTemplateNode = ConcreteTemplateNode
{ templateSelector :: Text
Expand All @@ -56,7 +57,7 @@ instance Yaml.FromJSON ConcreteTemplateNode where
case v of
String s -> return $ ConcreteTemplateNode "" "" s
Object o -> verboseForm o
_ -> fail "Template inválido"
_ -> fail "Invalid Template"
where
trimTrailingNewline "" = ""
trimTrailingNewline i =
Expand Down Expand Up @@ -86,7 +87,6 @@ newtype Template =
type MonadTex m = (MonadCatch m, MonadState TexState m, MonadIO m, MonadMask m)
type TexM = StateT TexState Identity


data TexState = TexState
{ tsFileName :: FilePath
, tsDebug :: Bool
Expand Down
19 changes: 19 additions & 0 deletions src/Text/JaTex/TemplateWrapper.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{-# LANGUAGE OverloadedStrings #-}
module Text.JaTex.TemplateWrapper
where

import Data.Aeson
import Data.Text (Text)
import qualified Data.Yaml as Yaml
import Text.JaTex.Template.Types (ConcreteTemplate)

data TemplateWrapper = TemplateWrapper
{ templateWrapperVersion :: Int
, templateWrapperExtends :: (Maybe Text)
, templateWrapperRules :: ConcreteTemplate
}

instance Yaml.FromJSON TemplateWrapper where
parseJSON (Object o) =
TemplateWrapper <$> o .: "version" <*> o .:? "extends" <*> o .: "rules"
parseJSON _ = fail "Invalid Template"
2 changes: 1 addition & 1 deletion src/Text/JaTex/TexWriter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ isTruthy (String s) = s /= mempty
isTruthy Null = False
isTruthy (Array _) = True

parseCTemplateFromJson :: Value -> Either [Text] [ConcreteTemplateNode]
parseCTemplateFromJson :: Value -> Either [Text] ConcreteTemplate
parseCTemplateFromJson (Object o) =
mergeEithers $ HashMap.foldrWithKey parsePair [] o
where
Expand Down
16 changes: 16 additions & 0 deletions src/Text/JaTex/UpgradeTemplate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.JaTex.UpgradeTemplate
where

import Data.Aeson
import qualified Data.Yaml as Yaml

run :: FilePath -> IO ()
run targetTemplateFile = do
minput <- Yaml.decodeFile targetTemplateFile :: IO (Maybe Value)
case minput of
Nothing -> error "Failed to parse input template"
Just !input ->
Yaml.encodeFile targetTemplateFile $
object ["version" .= (Number 1), "rules" .= input]

0 comments on commit 2be537a

Please sign in to comment.