Commit d0cfecb2 authored by Henrik Tramberend's avatar Henrik Tramberend
Browse files

Simplify id structure

parent 7e178325
......@@ -65,6 +65,7 @@ import Text.Pandoc.Walk
import Text.Pandoc.Writers.HTML (writeHtml5String)
import Text.Pandoc.Writers.Math (texMathToInlines)
import Text.Pandoc.Writers.Shared
import Debug.Trace
-- import Text.Pandoc.XML (toHtml5Entities)
type Notes = [[Block]]
......
......@@ -8,9 +8,9 @@ module Sketch
) where
import Common
import Markdown
import Resources
import Slide
import Markdown
import Control.Monad
import qualified Data.Text.IO as T
......@@ -23,16 +23,22 @@ idDigits = 4
-- | Selects a random id out of idDigits^36 possibilities
randomId :: IO String
randomId = ('s' :) <$> replicateM idDigits randomChar
randomId = do
h <- randomAlpha
t <- replicateM (idDigits - 1) randomAlphaNum
return $ h : t
-- | Rejection sampling for a random character from [0-9] or [a-z].
randomChar :: IO Char
randomChar = do
randomAlphaNum :: IO Char
randomAlphaNum = do
r <- getStdRandom (randomR ('0', 'z'))
if r > '9' && r < 'a'
then randomChar
then randomAlphaNum
else return r
randomAlpha :: IO Char
randomAlpha = getStdRandom (randomR ('a', 'z'))
-- | Writes a pandoc document to a markdown file.
writeToMarkdownFile :: FilePath -> Pandoc -> IO ()
writeToMarkdownFile filepath pandoc
......@@ -52,7 +58,8 @@ writeToMarkdownFile filepath pandoc
, writerColumns = 999
, writerSetextHeaders = False
}
runIO (Markdown.writeMarkdown options pandoc) >>= handleError >>= T.writeFile filepath
runIO (Markdown.writeMarkdown options pandoc) >>= handleError >>=
T.writeFile filepath
provideSlideIds :: Pandoc -> IO Pandoc
provideSlideIds (Pandoc meta body) = do
......
......@@ -4,6 +4,8 @@ module SketchTests
import Sketch
import Slide
import Data.Char
import Test.Hspec
import Text.Pandoc
import Text.Pandoc.Lens
......@@ -18,16 +20,16 @@ noHeaderSlide = Slide Nothing []
sketchTests = do
describe "randomId" $ do
it "creates a random id" $ length <$> randomId `shouldReturn` idDigits + 1
it "creates a random id that starts with 's'" $
head <$> randomId `shouldReturn` 's'
it "creates a random id" $ length <$> randomId `shouldReturn` idDigits
it "creates a random id that starts with a letter" $
isAlpha . head <$> randomId `shouldReturn` True
describe "provideSlideIdIO" $ do
it "adds new id to any slide that does not have one" $
length . view (header . _Just . attributes . attrIdentifier) <$>
provideSlideIdIO noIdSlide `shouldReturn` idDigits + 1
provideSlideIdIO noIdSlide `shouldReturn` idDigits
it "adds new header and id to any slide that does not have either" $
length . view (header . _Just . attributes . attrIdentifier) <$>
provideSlideIdIO noHeaderSlide `shouldReturn` idDigits + 1
provideSlideIdIO noHeaderSlide `shouldReturn` idDigits
it "does not touch headers that already have an id" $
view (header . _Just . attributes . attrIdentifier) <$>
provideSlideIdIO someIdSlide `shouldReturn` "manually-set-id"
---
decker-slide-ids: True
subtitle: High Quality Slide Annotations
title: Sketch Pad
---
......@@ -11,7 +12,7 @@ Some body text.
------
# {#sncdl}
# {#sc58j}
------
......
Supports Markdown
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