"""
A txt2tags entry formatter for pyblosxom.

Txt2tags (http://txt2tags.org) is a document generator. It reads a text file
with minimal markup such as **bold** and //italic// and converts it to the
following formats: HTML, XHTML, SGML, DocBook, LaTeX, Lout, Man page, Creole,
Wikipedia / MediaWiki, Google Code Wiki, PmWiki, DokuWiki, MoinMoin,
MagicPoint, PageMaker, AsciiDoc, ASCII Art and Plain text.

Install txt2tags and copy this file to your pyblosxom plugins directory. Entry
files with a .t2t extension will be parsed by this plugin.

You can configure this as your default preformatter for .txt files by
configuring it in your config file as follows::

    py['parser'] = 'txt2tags'

or in your blosxom .txt file entries, place a '#parser txt2tags' line after the
title of your blog::

    My Little Blog Entry
    #parser txt2tags
    My main story...

Further configuration options (default values set as follow):

    # Document encoding
    py['t2t_encoding'] = 'iso-8859-1'

    # Insert CSS-friendly tags
    py['t2t_css-sugar'] = 0

    # Show a table of contents
    py['t2t_toc'] = 0

    # Hide email adressess in HTML body
    py['t2t_mask-email'] = 0

    # Enumerate titles
    py['t2t_enum-title'] = 0
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright 2011 Tiago Bortoletto Vaz <tiago@debian.org>
"""

__version__ = ' 0.9 - Sun Jan 30 22:18:52 CET 2011'
__author__ = 'Tiago Bortoletto Vaz <tiago@debian.org>'


PREFORMATTER_ID = 'txt2tags'
FILE_EXT = 't2t'

from Pyblosxom import tools
import txt2tags


def cb_entryparser(args):
    args[FILE_EXT] = readfile
    return args


def cb_preformat(args):
    if args['parser'] == PREFORMATTER_ID:
        return parse(''.join(args['story']))
    else:
        return ''.join(args['story'])


def readfile(filename, request):
    entryData = {}
    pyblosxom_config = request.getConfiguration()
    lines = open(filename).readlines()

    if len(lines) == 0:
        return {"title": "", "body": ""}

    # Here is the marked body text, it must be a list.
    title = lines.pop(0)
    body  = lines
    
    # Set the configuration on the 'config' dict.
    config = txt2tags.ConfigMaster()._get_defaults()
    config['outfile']   = txt2tags.MODULEOUT  # results as list
    config['target']    = 'html'              # target type: HTML
    config['headers']   = 0

    # Configurable from pyblosxom config file
    config['encoding']   = pyblosxom_config.get('t2t_encoding', 'iso-8859-1')       # document encoding
    config['css-sugar']  = pyblosxom_config.get('t2t_css-sugar', 0)                 # CSS friendly
    config['toc']        = pyblosxom_config.get('t2t_toc', 0)                       # show Table Of Contents
    config['mask-email'] = pyblosxom_config.get('t2t_mask-email', 0)                # hide email adressess
    config['enum-title'] = pyblosxom_config.get('t2t_enum-title', 0)                # enumerate titles

    # Let's do the conversion
    body, toc = txt2tags.convert(body, config)
    toc       = txt2tags.toc_tagger(toc, config)
    toc       = txt2tags.toc_formatter(toc, config)
    full_doc  = toc + body
    body      = ''.join(txt2tags.finish_him(full_doc, config))
    
    entryData = {'title': title,
                 'body': body}

    # Call the postformat callbacks
    tools.run_callback('postformat',
            {'request': request,
             'entry_data': entryData})

    return entryData

