PATH_TO_MD = PATH + '/notes/' PATH_TO_HTML = PATH + "/engine/data/html/" PATH_TO_IMG = PATH + '/notes/' PATH_TO_ORIG = PATH + "/engine/data/orig/" AI_WRITER = False class Page(object):
    """Page class

    Attributes:

      fn - filename of the note, with .md
      md - md content of the note
      html - html content of the note

    """

    def __init__(self, fn):
        """Init a Page and load the content of MD file into self.md"""
        self.fn = fn
        # it catches errors if the file is removed
        try:
            with codecs.open(PATH_TO_MD + sep + fn, "r", "utf-8") as f:
                self.md = f.read()
            self.html = ''
        except IOError:
            # logging.error('file removed ' + self.fn)
            self.md = None

    def get_html(self):
        """Compile md to get html"""
        self.html = markdown.markdown(
            self.md, extensions=[GithubFlavoredMarkdownExtension(), 'footnotes'])  # (linenums=False)'])
        # html = '<link rel="stylesheet" href="/home/magnus/Dropbox/lb_v2/templates/Pygments/css/pygments.css" type="text/css">' + html

    def compile(self):
        """Preprocess, compile, postprocess.
        """
        logger.info('compiling --> %s' % self.fn)
        self.pre_process()
        self.get_html()
        self.post_process()

    def pre_process(self):
        """Do preprocessing.

        E.g.::

           self.md = get_image_path(self.md)"""
        self.md = update_upper_note(self.md)
        self.md = tablify_images(self.md)
        self.md = include_md_files(self.md)
        self.md = include_file(self.md)
        self.md = get_image_path(self.md)
        self.md = get_youtube_embeds(self.md)
        self.md = get_abstract(self.md)
        self.md = get_captions(self.md)
        self.md = make_interna_links(self.md)
        self.md = make_sport_links(self.md)
        self.md = misc_on_text(self.md)
        # self.md = right_link_from_dropbox_screenshot(self.md)

    def post_process(self):
        """Do postprocessing"""
        self.html = make_table_of_content(self.fn, self.html)
        self.html = add_head_for_flask(self.html)
        self.html = change_data_tag_into_actual_data(self.fn, self.html)
        self.html = add_path_to_img(self.html)
        self.html = change_html_tags_bootstrap(self.html)
        self.html = unhighlight(self.html)
        self.html = personal_tags_to_html(self.html)
        if FIND_FILES_PLUGIN:
            self.html = find_files(self.html)
        self.html = color_dna(self.html)
        self.html = get_todo(self.html)
        self.html = add_title(self.html, self.fn)
        self.html = use_icons(self.html)
        self.html = get_divhr(self.html)
        if ADD_EXTRA_SPACE_FOR_NOTES:
            self.html += "<div class='notes'></div>"

    def export(self, path, add_toc, push, readme):
        import re
        import shutil

        content = open(PATH_TO_MD + self.fn).read()
                       
        if self.fn == readme:
             self.fn = 'README.md'
        
        with open(path + os.sep + self.fn, 'w') as f:
            f.write(content)
        try:
            os.mkdir(path + os.sep + 'imgs')
        except:
            pass

        with open(path + os.sep + 'imgs/__place_for_your_imgs__', 'w') as f:
            f.write('__place_for_your_imgs__')

        # \!\[.*?\]\(imgs/.*?\)
        hits = re.findall('\"/imgs/.*?\"', content, re.M|re.DOTALL)
        for h in hits:
            print('Copy ',h.strip())
            shutil.copy(PATH_TO_MD + h.replace('"',''), path + os.sep + 'imgs')

        import subprocess

        def exe(cmd):
            o = subprocess.Popen(
                cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out = o.stdout.read().strip().decode()
            err = o.stderr.read().strip().decode()
            return out, err

        if add_toc:
            out, err = exe('gh-md-toc ' + path + os.sep + self.fn)

            with open(path + os.sep + self.fn) as f:
                content = f.read()

            ncontent = content.replace('{{TOC}}',out)

            with open(path + os.sep + self.fn, 'w') as f:
                 f.write(ncontent)

        if push:
            print('Push ...')
            out, err = exe("cd " + path + "&& git add *.md; git add imgs/* && git commit -m 'update' && git push")
            print(err)

    def is_changed(self):
        """Check if the file on disc is different than `md`.

        Make PATH_TO_ORIG if it does not exists.

        Return:
          boolean
        """
        # check if self.md exits, it does not exist if __ini__ failed (and it fails when the
        # file is removed
        if self.md:
            if not os.path.exists(PATH_TO_ORIG):
                os.makedirs(PATH_TO_ORIG)

            try:
                with codecs.open(PATH_TO_ORIG + sep + self.fn, "r", "utf-8") as f:
                    orig_md = f.read()
            except IOError:
                logging.error('file not detected. Create it: ' + self.fn)
                orig_md = ''
                pass

            if self.md != orig_md:
                copy(PATH_TO_MD + sep + self.fn, PATH_TO_ORIG + sep + self.fn)
                return True
            else:
                return False

    def save(self):
        """Save html file to the drive"""
        if not os.path.exists(PATH_TO_HTML):
            os.mkdir(PATH_TO_HTML)

        with codecs.open(PATH_TO_HTML + self.fn.replace('.md', '.html'), "w", "utf-8") as outfn:
            outfn.write(self.html) class Md_update(object):
    """Md_update class

    Attributes:

      fn
      md
    This class is responsible for the changes operated directly on the MD file.
    For example replacing some patterns with MarkDown language.
    """

    def __init__(self, fn):
        """Init a Page and load the content of MD file into self.md"""
        self.fn = fn
        try:
            with codecs.open(PATH_TO_MD + sep + fn, "r", "utf-8") as f:
                self.md = f.read()
        except IOError:
            # logging.error('file removed ' + self.fn)
            pass

    def compile(self):
        """Preprocess, compile, postprocess."""
        # get_ss
        self.md, is_get_ss = get_ss(self.md)

        # insert_image_in_md
        is_ii = False
        if SCREENSHOT_INBOX:
            self.md, is_ii = insert_image_in_md(self.md, PATH_TO_IMG, IMG_PREFIX)
        self.md, is_right_MD = right_MD_from_webservices(self.md)

        self.md, yti = get_youtube_embeds_insert(self.md)
        self.md, is_simply_interal_links = simply_interal_links(self.md)

        use_rm = False
        if USE_RM_TO_REMOVE_FIGURE:
            self.md, use_rm = remove_image(self.md)

        # ai writer
        is_edit_synatx_ai = False
        if AI_WRITER:
            self.md, is_edit_synatx_ai = edit_syntax_from_ai_writer_to_geekbook(self.md, IMG_PREFIX)

        # check if anything changed
        if any([is_get_ss, is_ii, is_right_MD, is_edit_synatx_ai, yti, use_rm,
                is_simply_interal_links]):
            return True
        else:
            return False

    def save(self):
        with codecs.open(PATH_TO_MD + sep + self.fn, "w", "utf-8") as outfn:
            outfn.write(self.md.strip()) class Index(object):
    def __init__(self):
        pass

    def update(self, list_md):
        """Update the index page

        :param list_md: is a list of your md files"""

        if FLASK_BASED:  # flask mode
            head = open(PATH_TO_TEMPLATE_HTML).read()
            head = head.replace('{{ url_index }}', PATH_TO_HTML + '/' + 'index.html')
            head = head.replace('href="img/', 'href="' + '/img/')
            head = head.replace('="lib/', '="' + '/lib/')
            head = head.replace('="css/', '="' + '/css/')
            head = head.replace('="js/', '="' + '/js/')

            # remove demo content
            head = re.sub(r'<!-- start of demo -->.*<!-- end of demo -->',
                          r'', head, flags=re.M | re.DOTALL)

            # insert dataTables  "sorting":true,
            head += """
<style>
body {
  background-color: black;
}</style>

              <!--      "paging": false,
                        -->
              <table id="table_id" class="display compact hover">
                  <thead>
                      <tr>
                          <th>Title</th>
                          <th>Last update</th>
                          <th style="text-align:center">#</th>
                      </tr>
                  </thead>
                  <tbody>
            """
            html = head
        else:
            html = open(PATH_TO_TEMPLATE + '/head.html').read()

            html = html.replace('{{ url_index }}', PATH_TO_HTML + '/' + 'index.html')

            # @todo
            html = html.replace('href="img/', 'href="' + PATH_TO_TEMPLATE + '/img/')
            html = html.replace('src="img/', 'src="' + PATH_TO_TEMPLATE + '/img/')
            html = html.replace('="lib/', '="' + PATH_TO_TEMPLATE + '/lib/')
            html = html.replace('="css/', '="' + PATH_TO_TEMPLATE + '/css/')

            # remove demo content
            html = re.sub(r'<!-- start of demo -->.*<!-- end of demo -->',
                          r'', html, flags=re.M | re.DOTALL)
            ##

        for mdfn in list_md:
            if mdfn == 'imgs':
                pass
            else:
                if mdfn.strip():
                    # Insert the description in the index
                    f = open(PATH_TO_MD + os.sep + mdfn)
                    lines = f.readlines()
                    f.seek(0)
                    if lines:  # if the file is empty
                        if lines[0][0] == '#':
                            desc = lines[0][1].strip() # if '# blelbe' -> 'bleble'
                        else:
                            desc = lines[0]
                    else:
                        desc = ''
                    for l in f:
                        # the tag will overwrite this before
                        if l.strip().startswith('[desc:'):
                            desc = l.replace('[desc:', '').replace(']', '').strip()


                    mdfn = re.sub('.md$', '', mdfn)  # replace only .md at the very end
                    path = PATH_TO_HTML + '/' + mdfn
                    # if l.find('::')>=0:
                    #    html += '<li class="table_of_content_h2">
                    # <a style="" href="' + path + '.html">' + l + '</a></li>'
                    # else:
                    if FLASK_BASED:
                        html += '<tr><td style=""><a class="index_list_a" href="/view/' + mdfn + '.html">' \
                                 + mdfn + '</a>' + '</br><span style="font-size:10px;color:gray">' + desc + '</td>' \
                                 + '<td style="white-space: nowrap;"><small><center class="index_date">' \
                                 + time.ctime(os.stat(os.path.join(PATH_TO_MD, mdfn + '.md')).st_mtime) \
                                            + '</center></small></td>' \
                                 + '<td><small><center class="index_date">' + str(len(lines)) + '</center>' \
                                 + '</small></td></tr>'


                    else:
                        html += '<tr><td><a class="index_list_a" href="' + path + '.html">' \
                                + mdfn + '</a></td>' + '<td>' + desc + '</td>' \
                                + '<td><small><center class="index_date">' + \
                                time.ctime(os.stat(os.path.join(PATH_TO_MD, mdfn + '.md')
                                                   ).st_mtime) + '</center></small></td></tr>'

        html += '</p>'

        html += "</tbody></table>"

        f = open(PATH_TO_HTML + 'index.html', 'w')
        f.write(html)
        f.close() class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92;1m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m' def make_db(verbose=False):
    db = Db()
    logger.info('searcher.py::making the db')
    db.collect_data(verbose)
    pickle.dump(db, open(PATH + os.sep + "engine/searchdb.pickle", "wb"), protocol=3) def add_timestamp_to_image(file):
def edit_syntax_from_ai_writer_to_geekbook(text, img_prefix):
import time
import os
import sys
import argparse
import logging
import gc
import platform
import subprocess
from engine.conf import PATH_TO_MD, PATH_TO_HTML, PATH_TO_IMG, PATH_TO_ORIG, AI_WRITER
from engine.page import Page
from engine.md_update import Md_update
from engine.make_index import Index
from engine.colors import bcolors
from engine.searcher import make_db
from engine.plugins import ia_writer
            os.mkdir(PATH_TO_ORIG)
        except OSError:
            pass

    def start(self):
        """Start the App.
        """
        if not self.args.debug:
            # os.system('clear')
            print (bcolors.OKGREEN + "\n                 ________               __   __________               __    \n                /  _____/  ____   ____ |  | _\______   \ ____   ____ |  | __\n               /   \  ____/ __ \_/ __ \|  |/ /|    |  _//  _ \ /  _ \|  |/ /\n               \    \_\  \  ___/\  ___/|    < |    |   (  <_> |  <_> )    < \n                \______  /\___  >\___  >__|_ \|______  /\____/ \____/|__|_ \ \n                       \/     \/     \/     \/       \/                   \/ \n" + bcolors.ENDC)
        logger.info("G33kB00k3 is Running... [ok]")

        logger.info("root path: %s" % PATH)
        try:
            os.makedirs(PATH_TO_HTML)
        except OSError:
            pass
        logger.info("html path: <file://" + PATH_TO_HTML + 'index.html>')
        logger.info("imgs path: " + PATH_TO_IMG)

        logger.info('Ready to go! Please edit me: notes/')

        o = subprocess.Popen('cd "' + PATH_TO_MD + '" && cat *md | wc -l', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out = o.stdout.read().strip().decode()

        mf = MdFiles()
        logger.info('You have %i notes' % len(mf.get_files()) + ' with ' + out + ' lines! Congrats, keep noting!')

        index = Index()
        print(mf.get_files())
        index.update(mf.get_files())

        # yappi.start()
        c = 0

        ipynb_mtime = {}

        while c < 10:  # for debugging
            # check for ipython

            # if not ipynb_mtime:
            # notebook_files = os.listdir(PATH_TO_MD)
            # for n in [n for n in notebook_files if n.endswith('.ipynb') and n.startswith('jupyter')]:
            # cmd = "jupyter nbconvert " + PATH_TO_MD + os.sep + n + " --to markdown"
            # print cmd
            # os.system(cmd)
            # ipynb_mtime[n] = os.path.getmtime(PATH_TO_MD + os.sep + n)
            # else:
            # notebook_files = os.listdir(PATH_TO_MD)
            # for n in [n for n in notebook_files if n.endswith('.ipynb')]:
            # if n in ipynb_mtime.keys():
            # mt = os.path.getmtime(PATH_TO_MD + os.sep + n)
            # if ipynb_mtime[n] < mt:
            # cmd = "jupyter nbconvert " + PATH_TO_MD + os.sep + n + " --to markdown"
            # print cmd
            # os.system(cmd)
            # ipynb_mtime[n] = mt
            # else:
            # mt = os.path.getmtime(PATH_TO_MD + os.sep + n)
            # cmd = "jupyter nbconvert " + PATH_TO_MD + os.sep + n + " --to markdown"
            # print cmd
            # os.system(cmd)
            # ipynb_mtime[n] = mt

            # see what's new - diff between to folders your notes and orig files that keep copy of our notes
            # grep -v removes things from your list, ~, # (and in mmagnus case org mode files)
            cmd = "diff -u -r \"" + PATH_TO_MD + "\" \"" + PATH_TO_ORIG + "\" " + \
                " | grep -v '\.org' | grep -v 'flycheck_' | grep -v '~$' | grep -v '#' | grep '\.md'".strip()
            out, err  = exe(cmd)
            if err:
                if 'No such file or directory' in err:
                    continue
                else:
                    raise Exception("Geekbook3 diff can't be completed")

            files_changed = []

            # pick all file names that are changed
            for l in out.split('\n'):
                # new notes
                if l.startswith('Only in ' + PATH_TO_MD):
                    files_changed.append(os.path.basename(l.split()[-1]))
                # changes notes
                if l.startswith('diff -u -r'):
                    files_changed.append(os.path.basename(l.split()[-1]))

            # if there are files change compile them
            for f in files_changed:
                m = Md_update(f)
                p = Page(f)
                if p.is_changed():
                    # if m is changed then (by using any of plugins working on markdown, run this
                    changed = m.compile()
                    if changed:  # only if something is changed in md
                        m.save()

                    p.compile()
                    p.save()

                    index = Index()
                    index.update(mf.get_files())

                    # update search db if any of the files
                    # is changed
                    make_db()

            if UPDATE:
                for f in mf.get_files():
                    if f == 'imgs':
                        pass
                    else:
                        p = Page(f)
                        p.compile()
                        p.save()

                sys.exit(0)

            # dev -d <file>
            if DEV:
                # update index

                index = Index()