--[[--
File              : latex.lua
Author            : Igor V. Sementsov <ig.kuzm@gmail.com>
Date              : 01.01.2024
Last Modified Date: 31.07.2024
Last Modified By  : Igor V. Sementsov <ig.kuzm@gmail.com>
--]]--
-- © 2008 David Given.
-- WordGrinder is licensed under the MIT open source license. See the COPYING
-- file in this distribution for the full text.

local untextab = {
	["#"] = "\\#",
	["$"] = "\\$",
	["&"] = "\\&",
	["{"] = "\\{",
	["}"] = "\\}",
	["_"] = "\\_{}",
	["^"] = "\\^{}",
	["~"] = "\\~{}",
	["%"] = "\\%",
	["<"] = "$\\langle$",
	[">"] = "$\\rangle$",
	["\\"] = "$\\backslash$"
}

local function untex(s)
	s = s:gsub("[#$&{}\\_^~%%<>]", untextab)
	return s
end

local style_tab =
{
	["H1"]     = {'\\section{',            '}'},
	["H2"]     = {'\\subsection{',         '}'},
	["H3"]     = {'\\subsubsection{',      '}'},
	["H4"]     = {'\\paragraph{',          '}'},
	["P"]      = {'\n',                     '\n'},
	["BOTH"]   = {'\n',                     '\n'},
	["L"]      = {'\\item[]{',             '}'},
	["LB"]     = {'\\item[\\textbullet]{', '}'},
	["LN"]     = {'\\item{',               '}'},
	["Q"]      = {'\\begin{quotation}\n',  '\n\\end{quotation}'},
	["V"]      = {'\\begin{quotation}\n',  '\n\\end{quotation}'},
	["RAW"]    = {'', ''},
	["PRE"]    = {'\\begin{verbatim}\n',  '\n\\end{verbatim}'},
	["LEFT"]   = {'\\begin{flushleft}\n', '\n\\end{flushleft}'},
	["RIGHT"]  = {'\\begin{flushright}\n', '\n\\end{flushright}'},
	["CENTER"] = {'\\begin{center}\n', '\n\\end{center}'},
}

local function callback(writer, document)
	return ExportFileUsingCallbacks(document,
	{
		prologue = function()
			writer('%% This document automatically generated by '..
				'WordGrinder '..VERSION..'.\n')
			writer('\\documentclass[a4paper, 12pt, oneside]{article}\n')
			writer('\\usepackage{setspace}\n')
			writer('\\usepackage[T2A]{fontenc}\n') 
			writer('\\usepackage[utf8]{inputenc}\n')
			writer('\\usepackage[english,russian]{babel}\n')
			writer('\\usepackage{indentfirst}\n')
			writer('\\usepackage{graphicx}\n')
			writer('\\usepackage{longtable}\n')
			writer('\\sloppy\n')
			writer('\\onehalfspacing\n')
			writer('\\begin{document}\n')
			writer('\\title{', untex(Document.name), '}\n')
			writer('\\author{(no author)}\n')
			--writer('\\maketitle\n')
		end,
		
		rawtext = function(s)
			writer(s)
		end,
		
		text = function(s)
			writer(untex(s))
		end,
		
		notext = function(s)
			writer('\\paragraph{}')
		end,
		
		italic_on = function()
			writer('\\textit{')
		end,
		
		italic_off = function()
			writer('}')
		end,
		
		bold_on = function()
			writer('\\textbf{')
		end,
		
		bold_off = function()
			writer('}')
		end,
		
		underline_on = function()
			writer('\\underline{')
		end,
		
		underline_off = function()
			writer('}')
		end,
		
		list_start = function()
			writer('\\begin{enumerate}\n')
		end,
		
		list_end = function()
			writer('\\end{enumerate}\n')
		end,
		
		paragraph_start = function(para)
			writer(style_tab[para.style][1] or "")
		end,		
		
		paragraph_end = function(para)
			writer(style_tab[para.style][2] or "")
			writer('\n')
		end,
		
		image_start = function(para)
			writer('\\begin{figure}[h]\n')
			writer('\\centering\n')
		end,
		
		image_end = function(para)
			writer('\n')
			writer(string.format('\\includegraphics[width=\\textwidth]{%s}\n\n', para.imagename))
			writer('\\end{figure}\n')
		end,

		table_start = function(para)
			writer('\\noindent\n\\begin{longtable}[]{')
			for cn, cell in ipairs(para.cells) do
				if para.style == "TRB" then
					writer('|')
				end
				writer(string.format('p{%spt}', para.cellWidth[cn] * 4.6))
			end
			if para.style == "TRB" then
				writer('|')
			end
			writer('}\n')
			if para.style == "TRB" then
				writer('\\hline\n')
			end
		end,
		
		table_end = function(para)
			writer('\\end{longtable}\n')
		end,
		
		tablerow_start = function(para)
		end,
		
		tablerow_end = function(para)
			writer('\\\\\n')
			if para.style == "TRB" then
				writer('\\hline\n')
			end
		end,
		
		tablecell_start = function(para, cn)
			if cn ~= 1 then
				writer('\n& ')
			end
		end,
		
		tablecell_end = function(para)
		end,
		
		epilogue = function()
			writer('\\end{document}\n')	
		end
	})
end

function Cmd.ExportLatexFile(filename)
	return ExportFileWithUI(filename, "Export LaTeX File", ".tex",
		callback)
end

function Cmd.ExportToLatexString()
	return ExportToString(Document, callback)
end

