Commit 2566d26e authored by Henrik Tramberend's avatar Henrik Tramberend
Browse files

Fix YAML processing (#3)

parent 278a474c
......@@ -6,6 +6,8 @@ subtitle: Tutorial and Examples
theme: 'htr-slides'
title: Decker Slide Tool
transition: linear
ümläüte: Ümläüte
signs: © €
---
# Overview
......@@ -261,6 +263,22 @@ total:
Your total score is 42.
```
# Encoding
## UTF-8 encoding for everything
- Markdown source files are assumed to be UTF-8 encoded
- YAML metadata also
## German Umlaute
- ÄÖÜäöüßß
## Substituted meta data
- Umlaute: {{ümläüte}}
- Signs: {{signs}}
# `decker` Tool {.section}
# `decker` Tool
......
......@@ -83,6 +83,7 @@ import System.Directory as Dir
import System.Exit
import System.FilePath as SF
import System.FilePath.Glob
import System.IO as S
import System.Process
import System.Process.Internals
import Text.CSL.Pandoc
......@@ -495,30 +496,31 @@ readMetaMarkdown :: FilePath -> Action Pandoc
readMetaMarkdown markdownFile = do
need [markdownFile]
-- read external meta data for this directory
metaDataExternal <- readMetaDataForDir (takeDirectory markdownFile)
externalMeta <- readMetaDataForDir (takeDirectory markdownFile)
-- extract embedded meta data from the document
(markdown, metaDataInternal) <- splitMarkdownFile markdownFile
markdown <- liftIO $ S.readFile markdownFile
let Pandoc meta _ = readMarkdownOrThrow pandocReaderOpts markdown
let documentMeta = MetaMap $ unMeta meta
-- combine the meta data with preference on the embedded data
let metaData = joinMeta metaDataInternal metaDataExternal
-- use mustache to substitute
let substituted = substituteMetaData markdown (MT.mFromJSON metaData)
-- liftIO $ Tio.putStrLn substituted
liftIO $ putStrLn $ T.unpack substituted
liftIO $ writeFile "test.md" $ T.unpack substituted
-- read the remaining markdown into pandoc
let pandoc =
case readMarkdown pandocReaderOpts (T.unpack substituted) of
Right (Pandoc _ blocks) -> Pandoc (toPandocMetaMeta metaData) blocks
Left err -> throw $ PandocException (show err)
let combinedMeta = mergePandocMeta documentMeta (toPandocMeta externalMeta)
let mustacheMeta = toMustacheMeta combinedMeta
-- use mustache to substitute
let substituted = substituteMetaData (T.pack markdown) mustacheMeta
-- read markdown with substitutions again
let Pandoc _ blocks =
readMarkdownOrThrow pandocReaderOpts $ T.unpack substituted
let (MetaMap m) = combinedMeta
let pandoc = Pandoc (Meta m) blocks
-- adjust image urls
projectDir <- getProjectDir
return $ walk (adjustImageUrls projectDir (takeDirectory markdownFile)) pandoc
where
readMarkdownOrThrow opts string =
case readMarkdown opts string of
Right pandoc -> pandoc
Left err -> throw $ PandocException (show err)
-- | Converts YAML meta data to Pandoc meta data
-- toMeta :: Y.Value -> Meta
-- toMeta yaml = case A.fromJSON (traceShowId yaml) of
-- A.Error msg -> throw $ YamlException msg
-- A.Success meta -> traceShowId $ Meta meta
toPandocMetaMeta :: Y.Value -> Meta
toPandocMetaMeta value =
case toPandocMeta value of
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment