]> git.wincent.com - docvim.git/commitdiff
Print TOC in Vim output
authorGreg Hurrell <greg@hurrell.net>
Thu, 9 Jun 2016 07:52:03 +0000 (00:52 -0700)
committerGreg Hurrell <greg@hurrell.net>
Thu, 9 Jun 2016 07:52:03 +0000 (00:52 -0700)
lib/Docvim/Printer/Vim.hs
tests/fixtures/vim/headings.golden
tests/fixtures/vim/integration-ferret-plugin.golden
tests/fixtures/vim/integration-pinnacle.golden

index b6178579c1eb9f5575d096fc6abb4ab96a6d91ae..37cb0f45e543558516a56810605ce4a7493951b0 100644 (file)
@@ -7,7 +7,7 @@ import Control.Monad.State
 import Data.Char (isSpace, toLower, toUpper)
 import Data.List (intercalate, isSuffixOf, span, sort)
 import Data.List.Split (splitOn)
-import Data.Maybe (fromMaybe)
+import Data.Maybe (fromJust, fromMaybe)
 import Data.Tuple (swap)
 import Docvim.AST
 import Docvim.Parse (rstrip)
@@ -152,6 +152,7 @@ node n = case n of
   Project p                  -> nodes p
   Separator                  -> append $ "---" ++ "\n\n"
   SubheadingAnnotation s     -> append $ s ++ " ~\n\n"
+  TOC t                      -> toc t
   Unit u                     -> nodes u
   Whitespace                 -> whitespace
   _                          -> append ""
@@ -183,6 +184,23 @@ listitem l = do
   where
     customLineBreak = "\n  "
 
+toc :: [String] -> Env
+toc t = do
+  metadata <- ask
+  h <- heading "contents"
+  entries <- append $ intercalate "\n" (format $ fromJust $ pluginName metadata) ++ "\n\n"
+  return $ concat [h, entries]
+  where
+    -- TODO: fix up this mess (very complicated by the need to thread through
+    -- `suffix` everywhere
+    format suffix            = map pad (zip (numbered suffix) (repeat suffix))
+    longest suffix           = maximum (map (\x -> length (snd x)) (numbered suffix))
+    numbered suffix          = map prefix (number suffix)
+    number suffix            = zip3 [1..] t (map (\x -> normalize $ x ++ "-" ++ suffix) t)
+    prefix (num, desc, l)    = (show num ++ ". " ++ desc ++ "  ", l)
+    pad ((lhs, rhs), suffix) = lhs ++ replicate (longest suffix - length lhs) ' ' ++ link rhs
+  -- TODO: consider doing this for markdown format too
+
 command :: Node -> Env
 command (CommandAnnotation name params) = do
   lhs <- append $ concat [":", name, " ", fromMaybe "" params]
@@ -248,8 +266,13 @@ heading h = do
   trailing <- append "\n"
   return $ concat [heading', target, trailing]
   where
-    target x = map (toLower . sanitize) $ x ++ "-" ++ h
-    sanitize x = if isSpace x then '-' else x
+    target x = normalize $ x ++ "-" ++ h
+
+normalize :: String -> String
+normalize = map (toLower . sanitize)
+
+sanitize :: Char -> Char
+sanitize x = if isSpace x then '-' else x
 
 link :: String -> String
 link l = "|" ++ l ++ "|"
index 71da9e70fea92a104198503b0b272b5bb86acae1..df473fc6cd3d463b862e29a3b32dfe685abcce24 100644 (file)
@@ -1,5 +1,11 @@
 *foo.txt*    We need this because otherwise we don't get link targets      *foo*
 
+CONTENTS                                                          *foo-contents*
+
+
+1. A heading                                                               |a-heading-foo|
+2. An overlength heading when I really should have chose something shorter  |an-overlength-heading-when-i-really-should-have-chose-something-shorter-foo|
+
 A HEADING                                                        *foo-a-heading*
 
 A subheading ~
index 38a1f607deaf082ebff44a1a62dac1bfe5b322de..b21bffeabf5a732c14f8c9a5409017dd9adb8019 100644 (file)
@@ -1,5 +1,23 @@
 *ferret.txt*    Ferret plug-in for Vim      *ferret*
 
+CONTENTS                                                       *ferret-contents*
+
+1. Intro                  |intro-ferret|
+2. Installation           |installation-ferret|
+3. Options                |options-ferret|
+4. Commands               |commands-ferret|
+5. Mappings               |mappings-ferret|
+6. Custom autocommands    |custom-autocommands-ferret|
+7. Overrides              |overrides-ferret|
+8. Troubleshooting        |troubleshooting-ferret|
+9. FAQ                    |faq-ferret|
+10. Related               |related-ferret|
+11. Website               |website-ferret|
+12. License               |license-ferret|
+13. Development           |development-ferret|
+14. Authors               |authors-ferret|
+15. History               |history-ferret|
+
 INTRO                                                             *ferret-intro*
 
     "ferret (verb)
index 21dc6b18e49f24eff82579e2baa3171016aba591..9534f9186235f02d451ca732316c7a2da8770bdc 100644 (file)
@@ -1,5 +1,15 @@
 *Pinnacle.txt*    Highlight group manipulation for Vim      *Pinnacle*
 
+CONTENTS                                                     *pinnacle-contents*
+
+1. Intro             |intro-pinnacle|
+2. Installation      |installation-pinnacle|
+3. Website           |website-pinnacle|
+4. License           |license-pinnacle|
+5. Development       |development-pinnacle|
+6. Authors           |authors-pinnacle|
+7. History           |history-pinnacle|
+
 INTRO                                                           *pinnacle-intro*
 
 Pinnacle provides functions for manipulating |:highlight| groups.