~jojo/Carth

ref: 2219ea535f4a0fd0afd9000ba5c6918c6b276210 Carth/src/Literate.hs -rw-r--r-- 853 bytes
2219ea53JoJo remove no longer applicable TODOs 7 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
{-# 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