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
|
% lhs2TeX --poly UnlitP.lhs > UnlitP.tex
\documentclass{article}
\usepackage[german]{babel}
%-------------------------------= --------------------------------------------
%include lhs2TeX.sty
%include lhs2TeX.fmt
%-------------------------------= --------------------------------------------
\begin{document}
%-------------------------------= --------------------------------------------
\section{\texttt{unlit}}
%-------------------------------= --------------------------------------------
Literate-Skripte, die Bird-Tracks verwenden, in Skripte "uberf"uhren,
die die Pseudo-\TeX-Kommandos \verb|\begin{code}|, \verb|\end{code}|
verwenden. Das Programm realisiert einen einfachen UNIX-Filter.
Synopsis:
%
\begin{verbatim}
unlit
unlit <file>
\end{verbatim}
%-------------------------------= --------------------------------------------
%if code || showModuleHeader
> module Main ( main )
> where
>
> import Char ( isSpace )
> import System ( getArgs )
> import Auxiliaries ( (.>) )
%endif
> data Class = Program LineNo Line
> | Spec LineNo Line
> | Blank LineNo Line
> | Comment LineNo Line
>
> type LineNo = Int
> type Line = String
> beginCode, endCode,
> beginSpec, endSpec :: Line
> beginCode = "\\begin{code}"
> endCode = "\\end\&{code}"
> beginSpec = "\\begin{spec}"
> endSpec = "\\end{spec}"
\NB Damit \verb|"\\end{code}"| nicht das Codesegment beendet, wird ein
Nullstring eingef"ugt: \verb|"\\end\&{code}"|.
> main :: IO ()
> main = do args <- getArgs
> unlit args
>
> unlit [] = getContents >>= (putStr . convert)
> unlit (filePath : _) = readFile filePath >>= (putStr . convert)
Fehlt: Fehlerbehandlung, wenn die Datei nicht vorhanden oder nicht
lesbar ist.
> convert :: String -> String
> convert = lines
> .> zip [1 ..] -- number
> .> map classify
> .> format
> .> unlines
> classify :: (LineNo, Line) -> Class
> classify (n, '>' : s) = Program n (' ' : s)
> classify (n, '<' : s) = Spec n (' ' : s)
> classify (n, s)
> | all isSpace s = Blank n s
> classify (n, s) = Comment n s
Die Formatierung wird mit einem einfachen endlichen Automaten mit f"unf
Zust"anden vorgenommen. Es wird darauf geachtet, da"s die Anzahl der
Zeilen nicht ver"andert wird.
\rightcolumn{52}%
> format :: [Class] -> [Line]
> format [] = []
> format (Program _ a : x) = (beginCode ++ a) : inProgram x
> format (Spec _ a : x) = (beginSpec ++ a) : inSpec x
> format (Blank _ a : x) = inBlank a x
> format (Comment _ a : x) = a : inComment x
\rightcolumn{45}%
\rightcolumn{37}%
> inBlank :: Line -> [Class] -> [Line]
> inBlank a [] = [a]
> inBlank a (Program _ b : x) = beginCode : b : inProgram x
> inBlank a (Spec _ b : x) = beginSpec : b : inSpec x
> inBlank a (Blank _ b : x) = a : inBlank b x
> inBlank a (Comment _ b : x) = a : b : inComment x
\rightcolumn{45}%
\rightcolumn{37}%
> inProgram :: [Class] -> [Line]
> inProgram [] = [endCode]
> inProgram (Program _ a : x) = a : inProgram x
> inProgram (Spec n a : x) = message n "program line" "specification line"
> inProgram (Blank _ a : x) = endCode : format x
> inProgram (Comment n a : x) = message n "program line" "comment"
\rightcolumn{45}%
\rightcolumn{37}%
> inSpec :: [Class] -> [Line]
> inSpec [] = [endSpec]
> inSpec (Program n a : x) = message n "specification line" "program line"
> inSpec (Spec _ a : x) = a : inSpec x
> inSpec (Blank _ a : x) = endSpec : format x
> inSpec (Comment n a : x) = message n "specification line" "comment"
> inComment :: [Class] -> [Line]
> inComment [] = []
> inComment (Program n a : x) = message n "comment" "program line"
> inComment (Spec n a : x) = message n "comment" "specification line"
> inComment (Blank _ a : x) = inBlank a x
> inComment (Comment _ a : x) = a : inComment x
> message :: LineNo -> String -> String -> a
> message n x y = error ("line " ++ show n ++ ": "
> ++ x ++ " next to " ++ y ++ "\n")
\end{document}
|