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

""" Convert Markdown file to html, which is embeded in html template.
"""

from __future__ import print_function, with_statement

import markdown
from jinja2 import Environment, FileSystemLoader

from simiki import configs


class Generator(object):
    def __init__(self, md_file):
        self.md_file = md_file

    def _get_title(self):
        """Get the wiki's title.

        Established:
            The first line of wiki is the title, written in html comment syntax.
            such as:
                <!-- title : The wiki title -->
        """
        notations = {"left" : "<!--", "right" : "-->"}
        with open(self.md_file, "rb") as fd:
            first_line = fd.readline().strip()

            for notation in notations.values():
                first_line = first_line.replace(notation, "")
            first_line = first_line.strip()

            # strip the space between `title` and `:`
            first_line = first_line.split(":", 1)
            first_line[0] = first_line[0].strip().lower()
            first_line = ":".join(first_line)

            title = first_line.lstrip("title:").strip()

            return title

    def _md2html(self, title):
        """Generate the html from md file, and embed it in html template."""
        md_file_text = unicode("".join(open(self.md_file, "rb").readlines()), \
                               "utf-8")
        content = markdown.markdown(md_file_text, \
                extensions=["fenced_code", "codehilite(guess_lang=False)"])

        env = Environment(loader = FileSystemLoader(configs.TPL_PATH))
        tpl_vars = {
            "wiki_name" : configs.WIKI_NAME,
            "wiki_keywords" : configs.WIKI_KEYWORDS,
            "wiki_description" : configs.WIKI_DESCRIPTION,
            "title" : title,
            "content" : content,
        }
        html = env.get_template('index.html').render(tpl_vars)

        return html

    def generator(self):
        title = self._get_title()
        html = self._md2html(title)

        return html

@@ -168,16 +168,46 @@
 tatement
+, unicode_literals%0A%0Aimport sys
 %0A%0Aimport
@@ -346,20 +346,40 @@
 elf, md_
+resource, md_type=%22
 file
+%22
 ):%0A     
@@ -385,30 +385,554 @@
     
-self.md_file = md_file
+%22%22%22%0A        :param md_resource: markdown resource, can be markdown file name or %0A            markdown text%0A        :param md_type: md_resource type, %60file%60 or %60text%60%0A        %22%22%22%0A        if md_type == %22file%22:%0A            with open(md_resource, %22rb%22) as fd:%0A                texts = %5Bunicode(_, %22utf-8%22) for _ in fd.readlines()%5D%0A                self.md_text = %22%22.join(texts)%0A        elif md_type == %22text%22:%0A            self.md_text = md_resource%0A        else:%0A            print(%22%60md_type%60 error, should be %60file%60 or %60text%60.%22)%0A            sys.exit(1)
 %0A%0A  
@@ -1231,91 +1231,57 @@
 %3E%22%7D%0A
-        with open(self.md_file, %22rb%22) as fd:%0A            first_line = fd.readline()
+%0A        first_line = self.md_text.split(%22%5Cn%22)%5B0%5D
 .str
@@ -1278,36 +1278,32 @@
 n%22)%5B0%5D.strip()%0A%0A
-    
         for nota
@@ -1334,36 +1334,32 @@
 ():%0A            
-    
 first_line = fir
@@ -1388,36 +1388,32 @@
 on, %22%22)%0A        
-    
 first_line = fir
@@ -1421,36 +1421,32 @@
 t_line.strip()%0A%0A
-    
         # strip 
@@ -1479,36 +1479,32 @@
 and %60:%60%0A        
-    
 first_line = fir
@@ -1525,36 +1525,32 @@
 %22:%22, 1)%0A        
-   
- 
 first_line%5B0%5D = 
@@ -1579,36 +1579,32 @@
 lower()%0A        
-    
 first_line = %22:%22
@@ -1626,28 +1626,24 @@
 e)%0A%0A        
- 
-   
 title = firs
@@ -1671,28 +1671,24 @@
 %22).strip()%0A%0A
-    
         retu
@@ -1801,19 +1801,17 @@
 emplate.
-%22%22%22
+%0A
 %0A       
@@ -1815,119 +1815,54 @@
     
-md_file_text = unicode(%22%22.join(open(self.md_file, %22rb%22).readlines()), %5C%0A                               %22utf-8%22)
+**Note** returned html is unicode.%0A        %22%22%22
 %0A   
@@ -1866,16 +1866,21 @@
         
+body_
 content 
@@ -1903,15 +1903,15 @@
 own(
-md_file
+self.md
 _tex
@@ -2294,16 +2294,21 @@
 tent%22 : 
+body_
 content,
@@ -2418,18 +2418,17 @@
  generat
-or
+e
 (self):%0A

add generate by markdown text