#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The MIT License (MIT)
#
# Copyright (c) 2013 Gil Gonçalves
#
# 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.

from sys import argv
from os import path, getcwd
from webbrowser import open as browser_open


MARKDOWN_EXTENSIONS = [ 'md' ]
REST_EXTENSIONS = [ 'rst' ]

TEMPLATE_CONTENTS = u"""
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>{filename}</title>
        <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            {file_contents}
        </div>
  </body>
</html>
"""


def bootstrap_text(filenames):
    if not len(filenames):
        raise TypeError('Needs at least one argument.')

    file_contents = ''
    for filename in filenames:
        with open(filename) as file:
            file_extension = filename[filename.rfind('.') + 1:]
            file_contents += text_to_html(
                file.read().decode('utf-8'),
                extension=file_extension,
            )

    return TEMPLATE_CONTENTS.format(
        filename=filename,
        file_contents=file_contents,
    )


def text_to_html(text, extension=None):
    if extension in MARKDOWN_EXTENSIONS:
        try:
            from markdown import markdown
        except ImportError:
            print "Couldn't import markdown."
            return text
        else:
            return markdown(text)
    elif extension in REST_EXTENSIONS:
        try:
            from docutils.core import publish_string
        except ImportError:
            print "Couldn't import docutils."
            return text
        else:
            return publish_string(text, writer_name='html')
    else:
        return text


if __name__=='__main__':
    if len(argv) < 2:
        raise TypeError('Needs at least one argument.')

    input_file = argv[1:]
    output_content = bootstrap_text(input_file)
    print output_content
