summaryrefslogtreecommitdiffstats
path: root/src/Main.hs
blob: 50903d4d7bdd7113190c8e7997b63c968b67c063 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
{-# LANGUAGE OverloadedRecordDot #-}

-- |
-- Module      : Main
-- Description : Command-line interface for org-zk
-- Copyright   : (c) 2023, Yann Herklotz
-- License     : GPL-3.0-only
-- Maintainer  : git [at] yannherklotz [dot] com
-- Stability   : experimental
-- Portability : POSIX
module Main where

-- import Data.Char (isAlphaNum, isLetter, isNumber)
-- import Data.Default (def)
-- import Data.List (nub)
-- import qualified Data.Text as T
-- import Data.Text.Read (decimal)
-- import Numeric (readInt, showIntAtBase)
-- import qualified Relude.Extra.Map as Map
-- import Text.Pandoc.Class (runIOorExplode)
-- import Text.Pandoc.Definition (Block (..), Inline (..), Pandoc(..), MetaValue(..), Meta(..))
-- import Text.Pandoc.Readers (readOrg)
-- import Text.Pandoc.Templates (compileTemplate, WithDefaultPartials(..), Template(..))
-- import Text.Pandoc.Writers (writeChunkedHTML, writeMarkdown)
-- import Text.Pandoc.Options (WriterOptions(..))
-- import Text.Pandoc.Chunks (splitIntoChunks, PathTemplate(..), ChunkedDoc(..), Chunk(..), toTOCTree, tocToList)
-- import Text.Pandoc.Shared (stringify)
-- import Text.Pandoc.Walk (walkM, walk)
-- import qualified Data.ByteString.Lazy as B
-- import System.Directory (createDirectoryIfMissing)

import Control.Exception (ioError)
import Control.Logging
import Data.Map.Strict (intersection)
import qualified Data.Text as T
import Data.Version (showVersion)
import Paths_org_zk (version)
import System.Console.GetOpt
import System.IO.Error (userError)
import Zettel

-- data HeaderState = HeaderState
--  { headerStateMap :: Map Text ((ZettelId, Text), [Text]),
--    headerStateCurrent :: Text
--  }

-- isLink :: Inline -> Bool
-- isLink Link {} = True
-- isLink _ = False
--
-- parseIds :: Text -> Text
-- parseIds t
--  | T.null t = ""
--  | T.head t == '#' && T.all isAlphaNum (T.tail t) = T.tail t
--  | otherwise = ""
--
-- addLinks :: [Inline] -> Block -> State HeaderState Block
-- addLinks il b = do
--  let f = filter isLink il
--  s <- get
--  let m' =
--        Map.insertWith
--          (\(_, y) (z, x) -> (z, x <> y))
--          (headerStateCurrent s)
--          ((mempty, mempty), filter (not . T.null) (parseIds . stringify <$> f))
--          (headerStateMap s)
--  put (HeaderState m' (headerStateCurrent s))
--  return b
--
-- getHeaders :: Block -> State HeaderState Block
-- getHeaders h@(Header _ (a, _, _) t) = do
--  s <- get
--  let m' = Map.insert a ((ZettelId a, stringify t), []) (headerStateMap s)
--  put (HeaderState m' a)
--  return h
-- getHeaders p@(Plain i) = addLinks i p
-- getHeaders p@(Para i) = addLinks i p
-- getHeaders p@(LineBlock i) = addLinks (concat i) p
-- getHeaders p = addLinks [] p
--
-- splitId :: ZettelId -> [Text]
-- splitId (ZettelId zid)
--  | T.null zid = []
--  | isNumber (T.head zid) = T.takeWhile isNumber zid : (splitId . ZettelId $ T.dropWhile isNumber zid)
--  | isLetter (T.head zid) = T.takeWhile isLetter zid : (splitId . ZettelId $ T.dropWhile isLetter zid)
--  | otherwise = []
--
-- combineId :: [Text] -> ZettelId
-- combineId = ZettelId . fold
--
-- intToDigit26 :: Int -> Char
-- intToDigit26 i
--  | i <= 25 && i >= 0 = toEnum $ fromEnum 'a' + i
--  | otherwise = error "Integer out of range."
--
-- digitToInt26 :: Char -> Int
-- digitToInt26 c = fromEnum c - fromEnum 'a'
--
-- fromBase :: Int -> String -> Maybe Int
-- fromBase base = fmap fst . viaNonEmpty head . readInt base ((< base) . digitToInt26) digitToInt26
--
-- toBase :: Int -> Int -> String
-- toBase base num = showIntAtBase base intToDigit26 num ""
--
-- opOnBase26 :: (Int -> Int) -> Text -> Maybe Text
-- opOnBase26 f t =
--  fromString . toBase 26 . f <$> fromBase 26 (toString t)
--
-- opOnIdPart :: (Int -> Int) -> Text -> Maybe Text
-- opOnIdPart f t
--  | T.null t = Nothing
--  | isNumber $ T.head t =
--      case decimal t of
--        Right (a, _) -> Just . show $ f a
--        _ -> Nothing
--  | isLetter $ T.head t = opOnBase26 f t
--  | otherwise = Nothing
--
-- defPredId :: ZettelId -> Maybe ZettelId
-- defPredId z =
--  case nonEmpty (splitId z) of
--    Just ne ->
--      if last ne == "a" || last ne == "1"
--        then Just (combineId (init ne))
--        else combineId . (init ne <>) . (: []) <$> opOnIdPart (subtract 1) (last ne)
--    Nothing -> Nothing
--
-- quoted :: Text -> Text
-- quoted t = "\"" <> t <> "\""
--
-- toDot :: [Text] -> (Text, [Text]) -> Text
-- toDot _ (t, l) = foldMap (\l' -> quoted l' <> " -> " <> quoted t <> ";\n") l
--
-- toDotNodes :: ZettelId -> Text
-- toDotNodes (ZettelId t)
--  | T.null t = ""
--  | isNumber $ T.head t = quoted t <> " [color=" <> T.singleton (T.head t) <> "];\n"
--  | otherwise = ""
--
-- addSelfLinks :: Map Text ((ZettelId, Text), [Text]) -> ZettelId -> Map Text ((ZettelId, Text), [Text])
-- addSelfLinks m z =
--  Map.insertWith (\((_, _), l') ((z', t), l) -> ((z', t), l ++ l')) (unZettelId z) ((mempty, mempty), [unZettelId z]) m
--
-- addSelfLinks2 :: ((ZettelId, b), [Text]) -> ((ZettelId, b), [Text])
-- addSelfLinks2 ((t, b), l) =
--  ((t, b), maybeToList (unZettelId <$> defPredId t) <> l)
--
-- decode :: ByteString -> Text
-- decode = decodeUtf8
--
-- normaliseHeadings :: Int -> Block -> Block
-- normaliseHeadings i (Header _ a b) = Header i a b
-- normaliseHeadings _ a = a
--
-- subtractHeadings :: Int -> Block -> Block
-- subtractHeadings i (Header current a b) = Header (current - i) a b
-- subtractHeadings _ a = a
--
-- removeHeadings :: Block -> Block
-- removeHeadings (Header _ _ _) = Plain []
-- removeHeadings a = a
--
-- mkLongPath :: ZettelId -> Text
-- mkLongPath i = fold . intersperse "/" $ splitId i
--
-- splitZettel :: Chunk -> Pandoc
-- splitZettel c =
--  walk removeHeadings
--    (Pandoc
--      (Meta
--        (fromList [("title", MetaInlines $ chunkHeading c)]))
--      (chunkContents c))
--
-- template :: Text
-- template = unlines ["+++",
--                    "title = \"$title$\"",
--                    "$if(date)$",
--                    "date = \"$date$\"",
--                    "$endif$",
--                    "+++",
--                    "",
--                    "$body$"]
--
-- main :: IO ()
-- main = do
--  let fl =
--        [ "/Users/ymherklotz/Dropbox/zk/verification.org",
--          "/Users/ymherklotz/Dropbox/zk/mathematics.org",
--          "/Users/ymherklotz/Dropbox/zk/hls.org",
--          "/Users/ymherklotz/Dropbox/zk/computing.org",
--          "/Users/ymherklotz/Dropbox/zk/hardware.org"
--        ]
--  fs <- mapM readFileBS fl
--  x <- mapM (runIOorExplode . readOrg def . decode) fs
--  let (_, s) = runState (forM x (walkM getHeaders)) (HeaderState mempty "toplevel")
--  let allZettel = second nub . addSelfLinks2 <$> headerStateMap s
--  writeFileText "out.dot" $
--    "digraph G {\noverlap=false;\nnode [colorscheme=pastel28,style=filled];\n"
--      <> foldMap (toDotNodes . fst . fst) allZettel
--      <> foldMap
--        ( toDot (foldMap snd allZettel)
--            . (\((ZettelId a, _), b) -> (a, b))
--        )
--        allZettel
--      <> "}\n"
--
--  let Just(firstDoc) = viaNonEmpty head x
----  print firstDoc
--  let res = splitIntoChunks "%i.md" False Nothing 15 firstDoc
--  Right templ <- runIOorExplode . runWithDefaultPartials $ compileTemplate "" template
--  forM_ (chunkedChunks res) $ \c -> do
--    print (chunkContents c)
--    let toc = case Map.lookup (chunkId c) allZettel of
--          Just toLinks ->
--            [BulletList (map (\x -> [Para [Link mempty [Str ("#" <> x)] ("/" <> x, mempty)]]) (snd toLinks))]
--          Nothing -> []
--    text <- runIOorExplode $ writeMarkdown (def { writerTemplate = Just templ }) (splitZettel c)
--    writeFileText ("test/content/" <> chunkPath c) text
--

data Options = Options
  { -- | Bibliography FILE (default: references.bib)
    optBibliography :: Maybe FilePath,
    -- | Bibliography FILE (default: bibliography.org)
    optOrgBibliography :: Maybe FilePath,
    -- | Csl FILE
    optCsl :: Maybe FilePath,
    -- | Index FILE (default: org-zettelkasten-index)
    optIndexFile :: Maybe FilePath,
    -- | Only process these indices
    optIndex :: [ZettelId],
    -- | output DIR
    optOutput :: Maybe FilePath,
    -- | Verbose output while processing files
    optVerbose :: Int,
    -- | Suppress all output
    optQuiet :: Bool,
    -- | Show current version
    optShowVersion :: Bool,
    -- | Show this help menu
    optShowHelp :: Bool
  }
  deriving (Show, Eq)

defaultOptions :: Options
defaultOptions =
  Options
    { optBibliography = Nothing,
      optOrgBibliography = Nothing,
      optCsl = Nothing,
      optIndexFile = Nothing,
      optIndex = [],
      optOutput = Nothing,
      optVerbose = 0,
      optQuiet = False,
      optShowVersion = False,
      optShowHelp = False
    }

parseIdList :: Text -> [ZettelId]
parseIdList = fmap ZettelId . T.splitOn ","

options :: [OptDescr (Options -> Options)]
options =
  [ Option
      ['b']
      ["bibliography"]
      (ReqArg (\f opts -> opts {optBibliography = Just f}) "FILE")
      "Bibliography FILE (default: references.bib)",
    Option
      ['c']
      ["csl"]
      (ReqArg (\f opts -> opts {optCsl = Just f}) "FILE")
      "Csl FILE",
    Option
      ['f']
      ["index-file"]
      (ReqArg (\f opts -> opts {optIndexFile = Just f}) "FILE")
      "Index FILE (default: org-zettelkasten-index)",
    Option
      ['h']
      ["help"]
      (NoArg (\opts -> opts {optShowHelp = True}))
      "Show this help menu",
    Option
      ['i']
      ["id"]
      ( ReqArg
          (\f opts -> opts {optIndex = opts.optIndex ++ parseIdList (T.pack f)})
          "ID"
      )
      "Only process these IDs",
    Option
      ['o']
      ["output"]
      (ReqArg (\f opts -> opts {optOutput = Just f}) "DIR")
      "output DIR",
    Option
      []
      ["org-bibliography"]
      (ReqArg (\f opts -> opts {optOrgBibliography = Just f}) "FILE")
      "Bibliography FILE (default: bibliography.org)",
    Option
      ['q']
      ["quiet"]
      (NoArg (\opts -> opts {optQuiet = True}))
      "Suppress all output",
    Option
      ['v']
      ["verbose"]
      (NoArg (\opts -> opts {optVerbose = optVerbose opts + 1}))
      "Verbose output while processing files",
    Option
      ['V', '?']
      ["version"]
      (NoArg (\opts -> opts {optShowVersion = True}))
      "Show current version"
  ]

versionStr :: String
versionStr = "org-zk " <> showVersion version <> " - (C) 2023 Yann Herklotz"

headerOpts :: String
headerOpts = versionStr <> "\n\nUsage: org-zk [OPTION...] [FILE...]\n\n[OPTION]"

compilerOpts :: [String] -> IO (Options, [String])
compilerOpts argv =
  case getOpt Permute options argv of
    (o, n, []) -> return (foldl' (flip id) defaultOptions o, n)
    (_, _, errs) -> ioError (userError (concat errs ++ usageInfo headerOpts options))

filterWithIds :: [ZettelId] -> ZettelGraph -> ZettelGraph
filterWithIds [] zg = zg
filterWithIds ids (ZettelGraph zidMap bibMap) =
  ZettelGraph (intersection zidMap zidMapFilter) (intersection bibMap bibMapFilter)
  where
    zidMapFilter = fromList $ map (\x -> (x, ())) ids
    bibMapFilter = fromList $ map (\(ZettelId t) -> (BibId t, ())) ids

main :: IO ()
main = withStderrLogging $ do
  (zkOpts, fl) <- getArgs >>= compilerOpts

  setLogLevel LevelWarn
  when (optVerbose zkOpts == 1) $ setLogLevel LevelInfo
  when (optVerbose zkOpts > 1) $ setLogLevel LevelDebug
  when (optQuiet zkOpts) $ setLogLevel LevelError

  debug $ "Options: " <> T.pack (show zkOpts)

  when (optShowHelp zkOpts) $ do
    putStrLn $ usageInfo headerOpts options
    exitSuccess

  when (optShowVersion zkOpts) $ do
    putStrLn versionStr
    exitSuccess

  graph <- parseZettelKasten (zip [1 ..] fl) (optOrgBibliography zkOpts)

  let pipeline = handleBibliography . wrapZettelGraph . linkAll . transcludeMdAll

  when (isNothing (optCsl zkOpts)) . warn $ "CSL file not set, using: ieee.csl"
  when (isNothing (optBibliography zkOpts)) . warn $ "Bibliography file not set, using: references.bib"

  renderZettelGraphFile
    (optVerbose zkOpts > 2)
    ( fromMaybe
        "ieee.csl"
        (optCsl zkOpts)
    )
    (optBibliography zkOpts)
    (fromMaybe "output" (optOutput zkOpts))
    (filterWithIds (optIndex zkOpts) $ pipeline graph)