~jojo/Carth

ref: 3f71c019bc3fd13360c650cd69e5e211660db158 Carth/src/Literate.hs -rw-r--r-- 872 bytes
3f71c019JoJo Delay StartNotDefined error until after typechecking 1 year, 10 months ago
                                                                                
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
{-# LANGUAGE LambdaCase #-}

module Literate (untangleOrg) where

import Data.Char
import Data.List

untangleOrg :: String -> String
untangleOrg s = unlines (untangleOrg' False (lines s))
  where
    untangleOrg' inSrc = \case
        [] -> []
        x : xs -> if inSrc
            then if endSrc x
                then "" : untangleOrg' False xs
                else x : untangleOrg' True xs
            else "" : untangleOrg' (beginSrc x) xs

beginSrc :: String -> Bool
beginSrc l =
    let ws = words l
    in
        (length ws >= 2)
        && (map toLower (ws !! 0) == "#+begin_src")
        && (ws !! 1 == "carth")
        && case elemIndex ":tangle" ws of
               Just i -> length ws >= i + 2 && ws !! (i + 1) == "yes"
               Nothing -> True

endSrc :: String -> Bool
endSrc = (\ws -> length ws > 0 && map toLower (ws !! 0) == "#+end_src") . words