Get rid of ugly ast, ast2, ast3 etc bindings
authorGreg Hurrell <greg@hurrell.net>
Sun, 12 Jun 2016 05:01:40 +0000 (22:01 -0700)
committerGreg Hurrell <greg@hurrell.net>
Sun, 12 Jun 2016 05:01:40 +0000 (22:01 -0700)
The replacement is pretty ugly too, so will look at refactoring further.

Closes: https://github.com/wincent/docvim/issues/11
lib/Text/Docvim/Compile.hs

index afb09bc14daf704ae65b3480afc08037f4810c0b..cd57bc5554c628359e9ca07e54385975d51bce00 100644 (file)
@@ -19,30 +19,26 @@ import Text.Docvim.Visitor.Section
 -- | "Compile" a set of translation units into a project.
 compile :: [Node] -> Node
 compile ns = do
-  let ast = foldr ($) (Project ns) [ injectCommands
-                                   , injectFunctions
-                                   , injectMappings
-                                   , injectOptions
-                                   ]
-  let (ast2, plugin) = extract extractPlugin ast
-  let (ast3, commands) = extract extractCommands ast2
-  let (ast4, command) = extract extractCommand ast3
-  let (ast5, functions) = extract extractFunctions ast4
-  let (ast6, function) = extract extractFunction ast5
-  let (ast7, mappings) = extract extractMappings ast6
-  let (ast8, mapping) = extract extractMapping ast7
-  let (ast9, options) = extract extractOptions ast8
-  let (ast10, option) = extract extractOption ast9
-  let (ast11, footer) = extract extractFooter ast10
-  optimize $ injectTOC $ Project $ concat [ plugin
-                               , [ast11]
-                               , commands
-                               , command
-                               , mappings
-                               , mapping
-                               , options
-                               , option
-                               , functions
-                               , function
-                               , footer
-                               ]
+    let ast = foldr ($) (Project ns) [ injectCommands
+                                     , injectFunctions
+                                     , injectMappings
+                                     , injectOptions
+                                     ]
+    let steps = [ extract extractPlugin
+                , extract extractCommands
+                , extract extractCommand
+                , extract extractMappings
+                , extract extractMapping
+                , extract extractOptions
+                , extract extractOption
+                , extract extractFunctions
+                , extract extractFunction
+                , extract extractFooter
+                ]
+    let (remainder, sections) = foldl reduce (ast, []) steps
+    let (beginning, rest) = splitAt 1 sections
+    optimize $ injectTOC $ Project $ (concat . concat) [beginning, [[remainder]], rest]
+  where
+    reduce (remainder', sections') step = do
+      let (r', s') = step remainder'
+      (r', sections' ++ [s'])