{-# LANGUAGE RankNTypes #-}

module Testing (module Utils,
                module Document.Decl,
                module HRef.Decl,
                module HRef.Grammars.Html,
                module Document.Semantics.Html,
                module HRef.Semantics.Html,
                module Document.Semantics.NumberedHeaders,
                module Document.Semantics.HtmlNumberedHeaders,
                module Preprocessors,
                module Language.Grammars.AspectAG,
                module Document.Grammars.Latex,
                module Toc.Semantics.Toc,
                module Testing) where

import Control.Applicative

import Utils
import Document.Decl
import HRef.Decl
import Toc.Decl
import Document.Grammars.Html
import HRef.Grammars.Html
import Document.Grammars.Markdown
import Toc.Grammars.Latex
import Document.Grammars.Latex
import Document.Semantics.Html
import HRef.Semantics.Html
import Document.Semantics.NumberedHeaders
import Toc.Semantics.Toc
import Toc.Semantics.Html
import Document.Semantics.HtmlNumberedHeaders
import Toc.Semantics.Html
import Preprocessors

import Language.Grammars.Murder
import Language.Grammars.AspectAG
import Language.Grammars.Murder.UUParsing
import Language.Grammars.Grammar

ex = do contents <- readFile "examples/inputLatex"
        writeFile "examples/output.html" (latex2html'' contents)

-- | Builds a converter tool from an extensible grammar fragment and
--   a label of the synthesized output.
buildConverter :: (HasField l syn v)
               => (forall env . ExtGram env (Record HNil -> syn) nts) 
               -> l 
               -> String 
               -> v
buildConverter gram att input = let parser = compile $ closeGram gram
                                    res    = result (parse parser input)
                                in  res emptyRecord # att

-- Examples of different latex to html converters

latex2html   = buildConverter (gLatex semHtml) html

latex2html'  = buildConverter (gLatex semHtml') html

latex2html'' = buildConverter (gLatex semHtml'' +>> gLatexToc semHtmlToc) html

test input = let parser = compile $ closeGram (gMarkdown undefined "#*=-")
             in  parse parser input

html2html input = let  parser = compile $ closeGram (gHtml semHtml)
                       x      = result (parse parser input)
                   in  x emptyRecord # html

-- example as
doc = Document [Header 1  [Plain "A document"],
                Paragraph [Plain "this is some ", Bold [Plain "really ", Italics [Plain "important"]], Plain " text."],
                Paragraph [Plain "mor text."]]