1%-------------------------------=  --------------------------------------------
2\subsection{Verbatim formatter}
3%-------------------------------=  --------------------------------------------
4
5%if codeOnly || showModuleHeader
6
7> module Verbatim               (  module Verbatim  )
8> where
9>
10> import Control.Arrow          (  (>>>) )
11> import Data.Char
12> import Data.List              (  intersperse )
13>
14> import Document
15> import Auxiliaries
16
17%endif
18
19% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
20\subsubsection{Inline and display code}
21% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
22
23The Boolean flag indicates whether a space should be typeset as \verb*| | (|True|) or not.
24
25> inline                        :: Bool -> String -> Doc
26> inline b                      =  latexs b >>> sub'verb
27>
28> display                       :: Int -> Bool -> String -> Doc
29> display width b               =   trim
30>                               >>> expand 0
31>                               >>> lines
32>                               >>> map (group width)
33>                               >>> map (map (latexs b))
34>                               >>> map splice
35>                               >>> intersperse sub'verbnl
36>                               >>> catenate
37>                               >>> sub'verbatim
38>
39> splice                        :: [Doc] -> Doc
40> splice ds                     =  Text "~" <<>> catenate (intersperse nl ds)
41>     where nl                  =  Text "!" <<>> sub'verbnl <<>> Text "!"
42>
43> latexs                        :: Bool -> String -> Doc
44> latexs b                      =  catenate . map latex
45>     where
46>     latex ' '
47>         | b                   =  Text "\\char32 "
48>     latex c
49>         | c `elem` " \t\n"    =  Text "~"
50>         | isAlphaNum c        =  Text [c]
51>         | otherwise           =  Text ("\\char" ++ show (fromEnum c) ++ "{}")
52
53ks, 11.01.2005: I've added {} after @\char@ to prevent ligatures like
54@--@ from applying.
55
56\NB Comments are \emph{not} typeset in \TeX, hence the name of the
57style. This is really a feature since the enclosed code need not be
58Haskell code.
59
60% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
61\subsubsection{Deleting blank lines and expanding tabs}
62% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
63
64Delete leading and trailing blank line(s).
65
66> trim                          :: String -> String
67> trim                          =  skip >>> reverse >>> skip >>> reverse
68>
69> skip                          :: String -> String
70> skip ""                       =  ""
71> skip s | all isSpace t        =  skip u
72>        | otherwise            =  s
73>        where (t, u)           =  breakAfter (== '\n') s
74
75Expanding tabs (assuming a tabulator width of $8$ characters).
76
77> expand                        :: Int -> String -> String
78> expand _n []                  =  []
79> expand _n ('\n' : s)          =  '\n' : expand 0 s
80> expand  n ('\t' : s)          =  replicate (n' - n) ' ' ++ expand n' s
81>     where n'                  =  (n + 8) `div` 8 * 8
82> expand  n (c : s)             =  c : expand (n + 1) s
83
84