auxiliaries for making the synopsis with multilingual examples

This commit is contained in:
aarne
2010-12-11 20:47:53 +00:00
parent da670babaa
commit bbb79126b8
5 changed files with 196 additions and 25 deletions

View File

@@ -1,7 +1,10 @@
import MkExxTable
import System
import Char
import List
import qualified Data.ByteString.Char8 as BS
import qualified Data.Map as M
import Debug.Trace ----
type Cats = [(String,String,String)]
type Rules = [(String,String,String)]
@@ -9,6 +12,9 @@ type Rules = [(String,String,String)]
-- the file generated
synopsis = "synopsis.txt"
apiExxFiles = ["api-examples-" ++ lang ++ ".txt" | lang <- words
"Bul Cat Dan Dut Eng Fin Fre Ger Ita Nor Pol Ron Spa Swe"]
main = do
xx <- getArgs
let isLatex = case xx of
@@ -18,8 +24,17 @@ main = do
cs2 <- getCats catAPI
let cs = sortCats (cs1 ++ cs2)
writeFile synopsis "GF Resource Grammar Library: Synopsis"
append "B. Bringert and A. Ranta"
append "B. Bringert, T. Hallgren, and A. Ranta"
space
append "%!Encoding:utf-8"
append "%!style(html): ./revealpopup.css"
space
append "%!postproc(html): '#divreveal' '<div class=reveal>'"
append "%!postproc(html): '#divpopup' '<div class=popup>'"
append "%!postproc(html): '#ediv' '</div>'"
append "%!postproc(html): '#UL' '<ul>'"
append "%!postproc(html): '#EUL' '</ul>'"
append "%!postproc(html): '#LI' '<li>'"
append "%!postproc(html): '(SRC=\"categories.png\")' '\\1 USEMAP=\"#categories\"'"
append "%!postproc(html): '#LParadigms' '<a name=\"RParadigms\"></a>'"
append "%!postproc(tex): '#LParadigms' ''"
@@ -42,13 +57,14 @@ main = do
space
link "Source 2:" structuralAPI
space
rs <- getRules syntaxAPI
apiExx <- getApiExx apiExxFiles
rs <- getRules apiExx syntaxAPI
--- putStrLn $ unlines ["p -cat=" ++ last (words t) ++
--- " \"" ++ e ++ "\"" | (_,t,e) <- rs, not (null e)] ----
rs2 <- getRules structuralAPI
rs2 <- getRules apiExx structuralAPI
let rss = rs ++ rs2
mapM_ putStrLn [f ++ " " ++ e | (f,_,e) <- rss]
delimit $ mkSplitTables True isLatex cs rss
--- mapM_ putStrLn [f ++ " " ++ e | (f,_,e) <- rss]
delimit $ mkSplitTables True isLatex apiExx cs rss
space
-- title "Structural Words"
-- space
@@ -66,8 +82,12 @@ main = do
space
include "synopsis-example.txt"
space
title "Table of Contents"
space
append "%%toc"
space
let format = if isLatex then "tex" else "html"
system $ "txt2tags -t" ++ format ++ " --toc " ++ synopsis
system $ "txt2tags -t" ++ format ++ " " ++ " --toc " ++ synopsis
if isLatex then (system $ "pdflatex synopsis.tex") >> return () else return ()
addToolTips :: Cats -> [String]
@@ -89,14 +109,14 @@ getCats file = do
(expl,ex) = span (/="e.g.") exp
_ -> getrs rs ss2
rulesTable :: Bool -> Bool -> Cats -> FilePath -> IO [String]
rulesTable hasEx isLatex cs file = do
rs <- getRules file
return $ mkTable hasEx isLatex cs rs
rulesTable :: ApiExx -> Bool -> Bool -> Cats -> FilePath -> IO [String]
rulesTable aexx hasEx isLatex cs file = do
rs <- getRules aexx file
return $ mkTable hasEx isLatex aexx cs rs
getRules :: FilePath -> IO Rules
getRules file = do
getRules :: ApiExx -> FilePath -> IO Rules
getRules aexx file = do
ss <- readFileC file >>= return . filter (not . hiddenLine) . lines
return $ getrs [] ss
where
@@ -127,7 +147,7 @@ putParadigms isLatex cs (lang,file) = do
space
link "source" file
space
rs <- rulesTable False isLatex cs file
rs <- rulesTable M.empty False isLatex cs file
space
delimit rs
space
@@ -139,32 +159,60 @@ inChunks i f = concat . intersperse ["\n\n"] . map f . chunks i where
-- Makes one table per result category.
-- Adds a subsection header for each table.
mkSplitTables :: Bool -> Bool -> Cats -> Rules -> [String]
mkSplitTables hasEx isLatex cs = concatMap t . addLexicalCats cs . sortRules
mkSplitTables :: Bool -> Bool -> ApiExx -> Cats -> Rules -> [String]
mkSplitTables hasEx isLatex aexx cs = concatMap t . addLexicalCats cs . sortRules
where t (c, xs) = [subtitle c expl] ++ tableOrLink
where
expl = case [e | (n,e,_) <- cs, n == c] of
[] -> ""
e:_ -> e
tableOrLink = if null xs then parad else mkTable hasEx isLatex cs xs
tableOrLink = if null xs then parad else mkTable hasEx isLatex aexx cs xs
parad = [
"Lexical category, constructors given in",
"[lexical paradigms #RParadigms]."
]
mkTable :: Bool -> Bool -> Cats -> Rules -> [String]
mkTable hasEx isLatex cs = inChunks chsize (\rs -> header : map (unwords . row) rs)
mkTable :: Bool -> Bool -> ApiExx -> Cats -> Rules -> [String]
mkTable hasEx isLatex aexx cs = inChunks chsize (\rs -> header : map (unwords . row) rs)
where
chsize = if isLatex then 40 else 1000
header = if hasEx then "|| Function | Type | Example ||"
else "|| Function | Type ||"
row (name,typ,ex)
= if hasEx then ["|", name', "|", typ', "|", ex', "|"]
else ["|", name', "|", typ', "|"]
row (name,typ,ex) =
let ntyp = mkIdent (name ++ " : " ++ typ) in
if hasEx then ["|", name', "|", typ', "|", ex' ntyp, "|"]
else ["|", name', "|", typ', "|"]
where
name' = ttf name
typ' = showTyp cs typ
ex' = if null ex then itf (takeWhile (/='_') name) else itf ex
ex' typ = if null ex then itf (takeWhile (/='_') name) else
case M.lookup typ aexx of
Just es -> mkExample es ex
_ -> trace typ $ itf ex
-- make an example with hover-popup translations
mkExample es ex = unwords [
"#divreveal",
itf ex,
"#divpopup",
"#UL",
unwords ["#LI" ++ e | e <- prApiEx es],
"#EUL",
"#ediv",
"#ediv"
]
-- makes mkUtt : QS -> Utt to mkUtt-QS-Utt
mkIdent :: String -> String
mkIdent = concatMap unspec where
unspec c = case c of
' ' -> ""
'>' -> ""
'(' -> ""
')' -> ""
':' -> "-"
_ -> [c]
mkCatTable :: Bool -> Cats -> [String]
mkCatTable isLatex cs = inChunks chsize (\rs -> header ++ map mk1 rs) cs