{-# LANGUAGE OverlappingInstances, FlexibleInstances, OverloadedStrings #-}{-# OPTIONS_GHC -fno-warn-orphans #-}{- Copyright (C) 2012 John MacFarlane <jgm@berkeley.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- |
Module : Text.Pandoc.Writers.Custom
Copyright : Copyright (C) 2012 John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : John MacFarlane <jgm@berkeley.edu>
Stability : alpha
Portability : portable
Conversion of 'Pandoc' documents to custom markup using
a lua writer.
-}moduleText.Pandoc.Writers.Custom(writeCustom)whereimportText.Pandoc.DefinitionimportText.Pandoc.OptionsimportData.List(intersperse)importData.Char(toLower)importScripting.Lua(LuaState,StackValue,callfunc)importqualifiedScripting.LuaasLuaimportText.Pandoc.UTF8(fromString,toString)importData.ByteString(ByteString)importqualifiedData.ByteString.Char8asC8importData.MonoidimportqualifiedData.MapasMattrToMap::Attr->M.MapByteStringByteStringattrToMap(id',classes,keyvals)=M.fromList$("id",fromStringid'):("class",fromString$unwordsclasses):map(\(x,y)->(fromStringx,fromStringy))keyvalsgetList::StackValuea=>LuaState->Int->IO[a]getListluai'=docontinue<-Lua.nextluai'ifcontinuethendonext<-Lua.peeklua(-1)Lua.poplua1x<-maybe(fail"peek returned Nothing")returnnextrest<-getListluai'return(x:rest)elsereturn[]instanceStackValueByteStringwherepushlx=Lua.pushl$C8.unpackxpeekln=(fmap.fmap)C8.pack(Lua.peekln)valuetype_=Lua.TSTRINGinstanceStackValuea=>StackValue[a]wherepushluaxs=doLua.createtablelua(lengthxs+1)0letaddValue(i,x)=Lua.pushluax>>Lua.rawsetilua(-2)imapM_addValue$zip[1..]xspeekluai=dotop<-Lua.gettoplualeti'=ifi<0thentop+i+1elseiLua.pushnillualst<-getListluai'Lua.poplua1return(Justlst)valuetype_=Lua.TTABLEinstanceStackValueFormatwherepushlua(Formatf)=Lua.pushlua(maptoLowerf)peekln=fmapFormat`fmap`Lua.peeklnvaluetype_=Lua.TSTRINGinstance(StackValuea,StackValueb)=>StackValue(M.Mapab)wherepushluam=doletxs=M.toListmLua.createtablelua(lengthxs+1)0letaddValue(k,v)=Lua.pushluak>>Lua.pushluav>>Lua.rawsetlua(-3)mapM_addValuexspeek__=undefined-- not needed for our purposesvaluetype_=Lua.TTABLEinstance(StackValuea,StackValueb)=>StackValue(a,b)wherepushlua(k,v)=doLua.createtablelua20Lua.pushluakLua.pushluavLua.rawsetlua(-3)peek__=undefined-- not needed for our purposesvaluetype_=Lua.TTABLEinstanceStackValue[Inline]wherepushlils=Lua.pushl.C8.unpack=<<inlineListToCustomlilspeek__=undefinedvaluetype_=Lua.TSTRINGinstanceStackValue[Block]wherepushlils=Lua.pushl.C8.unpack=<<blockListToCustomlilspeek__=undefinedvaluetype_=Lua.TSTRINGinstanceStackValueMetaValuewherepushl(MetaMapm)=Lua.pushlmpushl(MetaListxs)=Lua.pushlxspushl(MetaBoolx)=Lua.pushlxpushl(MetaStrings)=Lua.pushlspushl(MetaInlinesils)=Lua.pushlilspushl(MetaBlocksbs)=Lua.pushlbspeek__=undefinedvaluetype(MetaMap_)=Lua.TTABLEvaluetype(MetaList_)=Lua.TTABLEvaluetype(MetaBool_)=Lua.TBOOLEANvaluetype(MetaString_)=Lua.TSTRINGvaluetype(MetaInlines_)=Lua.TSTRINGvaluetype(MetaBlocks_)=Lua.TSTRING-- | Convert Pandoc to custom markup.writeCustom::FilePath->WriterOptions->Pandoc->IOStringwriteCustomluaFileoptsdoc=doluaScript<-readFileluaFilelua<-Lua.newstateLua.openlibsluaLua.loadstringlualuaScript"custom"Lua.calllua00-- TODO - call hierarchicalize, so we have that inforendered<-docToCustomluaoptsdocLua.closeluareturn$toStringrendereddocToCustom::LuaState->WriterOptions->Pandoc->IOByteStringdocToCustomluaopts(Pandoc(Metametamap)blocks)=dobody<-blockListToCustomluablockscallfunclua"Doc"bodymetamap(writerVariablesopts)-- | Convert Pandoc block element to Custom.blockToCustom::LuaState-- ^ Lua state->Block-- ^ Block element->IOByteStringblockToCustom_Null=return""blockToCustomlua(Plaininlines)=callfunclua"Plain"inlinesblockToCustomlua(Para[Imagetxt(src,tit)])=callfunclua"CaptionedImage"srctittxtblockToCustomlua(Parainlines)=callfunclua"Para"inlinesblockToCustomlua(RawBlockformatstr)=callfunclua"RawBlock"format(fromStringstr)blockToCustomluaHorizontalRule=callfunclua"HorizontalRule"blockToCustomlua(Headerlevelattrinlines)=callfunclua"Header"levelinlines(attrToMapattr)blockToCustomlua(CodeBlockattrstr)=callfunclua"CodeBlock"(fromStringstr)(attrToMapattr)blockToCustomlua(BlockQuoteblocks)=callfunclua"BlockQuote"blocksblockToCustomlua(Tablecaptalignswidthsheadersrows')=callfunclua"Table"capt(mapshowaligns)widthsheadersrows'blockToCustomlua(BulletListitems)=callfunclua"BulletList"itemsblockToCustomlua(OrderedList(num,sty,delim)items)=callfunclua"OrderedList"itemsnum(showsty)(showdelim)blockToCustomlua(DefinitionListitems)=callfunclua"DefinitionList"itemsblockToCustomlua(Divattritems)=callfunclua"Div"items(attrToMapattr)-- | Convert list of Pandoc block elements to Custom.blockListToCustom::LuaState-- ^ Options->[Block]-- ^ List of block elements->IOByteStringblockListToCustomluaxs=doblocksep<-callfunclua"Blocksep"bs<-mapM(blockToCustomlua)xsreturn$mconcat$intersperseblocksepbs-- | Convert list of Pandoc inline elements to Custom.inlineListToCustom::LuaState->[Inline]->IOByteStringinlineListToCustomlualst=doxs<-mapM(inlineToCustomlua)lstreturn$C8.concatxs-- | Convert Pandoc inline element to Custom.inlineToCustom::LuaState->Inline->IOByteStringinlineToCustomlua(Strstr)=callfunclua"Str"$fromStringstrinlineToCustomluaSpace=callfunclua"Space"inlineToCustomlua(Emphlst)=callfunclua"Emph"lstinlineToCustomlua(Stronglst)=callfunclua"Strong"lstinlineToCustomlua(Strikeoutlst)=callfunclua"Strikeout"lstinlineToCustomlua(Superscriptlst)=callfunclua"Superscript"lstinlineToCustomlua(Subscriptlst)=callfunclua"Subscript"lstinlineToCustomlua(SmallCapslst)=callfunclua"SmallCaps"lstinlineToCustomlua(QuotedSingleQuotelst)=callfunclua"SingleQuoted"lstinlineToCustomlua(QuotedDoubleQuotelst)=callfunclua"DoubleQuoted"lstinlineToCustomlua(Cite_lst)=callfunclua"Cite"lstinlineToCustomlua(Codeattrstr)=callfunclua"Code"(fromStringstr)(attrToMapattr)inlineToCustomlua(MathDisplayMathstr)=callfunclua"DisplayMath"(fromStringstr)inlineToCustomlua(MathInlineMathstr)=callfunclua"InlineMath"(fromStringstr)inlineToCustomlua(RawInlineformatstr)=callfunclua"RawInline"format(fromStringstr)inlineToCustomlua(LineBreak)=callfunclua"LineBreak"inlineToCustomlua(Linktxt(src,tit))=callfunclua"Link"txt(fromStringsrc)(fromStringtit)inlineToCustomlua(Imagealt(src,tit))=callfunclua"Image"alt(fromStringsrc)(fromStringtit)inlineToCustomlua(Notecontents)=callfunclua"Note"contentsinlineToCustomlua(Spanattritems)=callfunclua"Span"items(attrToMapattr)