From af77755668b7a3dc3f6bcf9c0bbf7e01852ab670 Mon Sep 17 00:00:00 2001 From: Jason Creighton Date: Sun, 27 May 2007 08:29:14 +0200 Subject: Generate keybindings section in manpage from Config.hs darcs-hash:20070527062914-b9aa7-d28805de3a198f81ca54cb800250bb16f65ab036 --- util/GenerateManpage.hs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 util/GenerateManpage.hs (limited to 'util') diff --git a/util/GenerateManpage.hs b/util/GenerateManpage.hs new file mode 100644 index 0000000..540c74c --- /dev/null +++ b/util/GenerateManpage.hs @@ -0,0 +1,47 @@ +-- +-- Generates man/xmonad.1 from man/xmonad.1.in by filling the list of +-- keybindings with values scraped from Config.hs +-- +-- Format for the docstrings in Config.hs takes the following form: +-- +-- -- mod-x @@ Frob the whatsit +-- +-- "Frob the whatsit" will be used as the description for keybinding "mod-x" +-- +-- If the keybinding name is omitted, it will try to guess from the rest of the +-- line. For example: +-- +-- [ ((modMask .|. shiftMask, xK_Return), spawn "xterm") -- @@ Launch an xterm +-- +-- Here, mod-shift-return will be used as the keybinding name. +-- +import Control.Monad +import Text.Regex.Posix +import Data.Char +import Data.List + +trim :: String -> String +trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace + +guessKeys line = concat $ intersperse "-" (modifiers ++ [map toLower key]) + where modifiers = map (!!1) (line =~ "(mod|shift|control)Mask") + (_, _, _, [key]) = line =~ "xK_(\\w+)" :: (String, String, String, [String]) + +binding :: [String] -> (String, String) +binding [ _, bindingLine, "", desc ] = (guessKeys bindingLine, desc) +binding [ _, _, keyCombo, desc ] = (keyCombo, desc) + +allBindings :: String -> [(String, String)] +allBindings xs = map (binding . map trim) (xs =~ "(.*)--(.*)@@(.*)") + +-- FIXME: What escaping should we be doing on these strings? +troff :: (String, String) -> String +troff (key, desc) = ".IP \\fB" ++ key ++ "\\fR\n" ++ desc ++ "\n" + +replace :: Eq a => a -> a -> [a] -> [a] +replace x y = map (\a -> if a == x then y else a) + +main = do + troffBindings <- (concatMap troff . allBindings) `liftM` readFile "./Config.hs" + let sed = unlines . replace "___KEYBINDINGS___" troffBindings . lines + readFile "./man/xmonad.1.in" >>= return . sed >>= writeFile "./man/xmonad.1" -- cgit v1.2.3