# coding=utf8

"""
    md2pdf.generator
    ~~~~~~~~~~~~~~~~

    Usage::

        >>> from md2pdf.generator import generator
        >>> generator.generate('sample.md', stylesheet='style.css')
"""

import os
import sys
import time
import subprocess

from md2pdf import default_stylesheet, charset
from md2pdf.parser import parser
from md2pdf.renderer import renderer
from md2pdf.utils import path_to


class Generator(object):

    def __init__(self):
        self.commands = ['wkhtmltopdf', '-']
        self.default_stylesheet = path_to('res', default_stylesheet)

    def generate(self, filepath, stylesheet=None, output=None):
        if stylesheet is None:
            stylesheet = self.default_stylesheet

        style = open(stylesheet).read()
        style = style.decode(charset)

        # update the output path if not output
        if output is None:
            # default: filename.pdf
            filename = os.path.basename(filepath)
            output = os.path.splitext(filename)[0] + '.pdf'
        self.commands.append(output)

        start_time = time.time()  # record execute duration

        # open and read source file
        markdown = open(filepath).read()

        # cast to unicode, jinja eats only unicode
        markdown = markdown.decode(charset)

        # parse markdown
        parser.set_source_path(filepath)
        html = parser.parse(markdown)

        # render with template
        html = renderer.render(html=html, style=style)

        # open a process to generate pdf
        proc = subprocess.Popen(self.commands, stdin=subprocess.PIPE,
                                stdout=sys.stdout, stderr=sys.stderr)
        stdout, stderr = proc.communicate(input=html.encode(charset))
        # calc execute time
        end_time = time.time()
        duration = end_time - start_time
        return (output, duration)


generator = Generator()

@@ -559,16 +559,53 @@
 lesheet)
+%0A        self.convert_to_html = False
 %0A%0A    de
@@ -1046,473 +1046,458 @@
 df'%0A
-        self.commands.append(output)%0A%0A        start_time = time.time()  # record execute duration%0A%0A        # open and read source file%0A        markdown = open(filepath).read()%0A%0A        # cast to unicode, jinja eats only unicode%0A        markdown = markdown.decode(charset)%0A%0A        # parse markdown%0A        parser.set_source_path(filepath)%0A        html = parser.parse(markdown)%0A%0A        # render with template%0A        html = renderer.render(html=html, style=style)%0A%0A
+%0A        prefix, suffix = os.path.splitext(output)%0A%0A        if suffix.endswith('html'):%0A            self.convert_to_html = True%0A%0A        self.commands.append(output)%0A%0A        start_time = time.time()  # record execute duration%0A%0A        # convert to html%0A        html = self.generate_html(filepath, style=style)%0A%0A        if self.convert_to_html:%0A            with open(output, 'w') as f:%0A                f.write(html.encode(charset))%0A        else:%0A    
     
@@ -1529,24 +1529,28 @@
 enerate pdf%0A
+    
         proc
@@ -1635,24 +1635,28 @@
             
+    
 stdout=sys.s
@@ -1685,24 +1685,28 @@
 rr)%0A        
+  
+  
 stdout, stde
@@ -1747,32 +1747,33 @@
 ncode(charset))%0A
+%0A
         # calc e
@@ -1891,16 +1891,454 @@
 ation)%0A%0A
+    def generate_html(self, filepath, style=None):%0A        # open and read source file%0A        markdown = open(filepath).read()%0A%0A        # cast to unicode, jinja eats only unicode%0A        markdown = markdown.decode(charset)%0A%0A        # parse markdown%0A        parser.set_source_path(filepath)%0A        html = parser.parse(markdown)%0A%0A        # render with template%0A        html = renderer.render(html=html, style=style)%0A%0A        return html%0A%0A
 %0Agenerat

add support for html output