summaryrefslogtreecommitdiff
path: root/src/Weave.hs
blob: f2d783621678271f9ad5d7fed69d18dbe7d221c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{- Copyright © 2015 Benjamin Barenblat

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>. -}

module Weave (weave) where

import Fragment

weave :: [Fragment] -> Either String String
weave = Right . concatMap weaveFragment

weaveFragment :: Fragment -> String
weaveFragment (Documentation text) = text
weaveFragment (BlockCode name body) =
  "\\begin{LytBlockCode}{" ++ name ++ "}\n"
  ++ weaveBlockBody body
  ++ "\\end{LytBlockCode}\n"

weaveBlockBody :: [CodeOrReference] -> String
weaveBlockBody ((Code first):rest) =
  {- The first block is code.  To make sure it gets typeset correctly, drop
  everything up to the first newline or non-space character. -}
  (case dropWhile (==' ') first of
     '\n' : first' -> first'
     first' -> first')
  ++ concatMap weaveCodeOrReference rest
weaveBlockBody blocks = concatMap weaveCodeOrReference blocks

weaveCodeOrReference :: CodeOrReference -> String
weaveCodeOrReference (Code text) = text
weaveCodeOrReference (Reference name) = "\\LytFragmentReference{" ++ name ++ "}"