Пример #1
0
def process_metadata(meta):
    for k, v in meta.items():
        if isinstance(v, list):
            meta[k] = [strip_p(compile_markdown(i)) for i in v]
        elif isinstance(v, str):
            meta[k] = strip_p(compile_markdown(v))
    return meta
Пример #2
0
def compile_page(path):
    """compile a markdown page"""
    with open(path, 'r') as f:
        raw = f.read()

    parts = path.split('/')
    slug = parts[-1].replace('.md', '')

    raw, meta = extract_metadata(raw)
    html = compile_markdown(raw)
    text = remove_html(html)
    desc = get_description(text)
    imgs = get_images(raw)

    data = {
        'plain': raw,
        'html': html,
        'text': text,
        'slug': slug,
        'description': desc,
        'template': 'page.html',
        'url': '/{}'.format(slug),
        'image': imgs[0] if imgs else None
    }
    data.update(meta)

    return Bunch(**data)
Пример #3
0
def compile_note(note, outdir, stylesheet=None, templ='default', ignore_missing=False, comments=False, preview=False):
    title, _ = os.path.basename(note).rsplit('.', 1)
    content = open(note, 'r').read()
    if templ.endswith('.html'):
        templ = env.from_string(open(templ, 'r').read())
    else:
        templ = env.get_template('{}.html'.format(templ))

    # create output directory if necessary
    outdir = os.path.join(outdir, title)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # create assets directory if necessary
    assetsdir = os.path.join(outdir, 'assets')
    if not os.path.exists(assetsdir):
        os.makedirs(assetsdir)

    # copy over any local images
    for img_path in parsers.md_images(content):
        # assume http indicates remote image
        if img_path.startswith('http'):
            continue

        # copy img to compiled assets directory
        _, img_name = os.path.split(img_path)
        to_img_path = os.path.join(assetsdir, img_name)
        from_img_path = os.path.join(os.path.dirname(note), img_path)
        try:
            shutil.copy(from_img_path, to_img_path)

            # update references to that image in the note
            to_img_path_rel = os.path.relpath(to_img_path, outdir)
            content = content.replace(img_path, to_img_path_rel)
        except FileNotFoundError:
            if not ignore_missing:
                raise
            print('Couldn\'t find `{}`, ignoring'.format(from_img_path))

    # default styles
    styles = open(os.path.join(templ_dir, 'style.css'), 'r').read()

    # if a stylesheet was specified, copy it over
    if stylesheet is not None:
        styles = '\n'.join([styles, open(stylesheet, 'r').read()])

    # write the stylesheet
    with open(os.path.join(outdir, 'style.css'), 'w') as f:
        f.write(styles)

    # render
    html = md2html.compile_markdown(content, comments=comments)
    content = templ.render(html=html, title=title, preview=preview)

    # save it
    outpath = os.path.join(outdir, 'index.html')
    with open(outpath, 'w') as out:
        out.write(content)

    return outpath
Пример #4
0
def compile_note(note, outdir, stylesheet=None, templ='default', ignore_missing=False):
    title, _ = os.path.basename(note).rsplit('.', 1)
    content = open(note, 'r').read()
    templ = env.get_template('{}.html'.format(templ))

    # create output directory if necessary
    outdir = os.path.join(outdir, title)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # create assets directory if necessary
    assetsdir = os.path.join(outdir, 'assets')
    if not os.path.exists(assetsdir):
        os.makedirs(assetsdir)

    # copy over any local images
    for img_path in parsers.md_images(content):
        # assume http indicates remote image
        if img_path.startswith('http'):
            continue

        # copy img to compiled assets directory
        _, img_name = os.path.split(img_path)
        to_img_path = os.path.join(assetsdir, img_name)
        from_img_path = os.path.join(os.path.dirname(note), img_path)
        try:
            shutil.copy(from_img_path, to_img_path)

            # update references to that image in the note
            to_img_path_rel = os.path.relpath(to_img_path, outdir)
            content = content.replace(img_path, to_img_path_rel)
        except FileNotFoundError:
            if not ignore_missing:
                raise
            print('Couldn\'t find `{}`, ignoring'.format(from_img_path))

    # default styles
    styles = open(os.path.join(templ_dir, 'style.css'), 'r').read()

    # if a stylesheet was specified, copy it over
    if stylesheet is not None:
        styles = '\n'.join([styles, open(stylesheet, 'r').read()])

    # write the stylesheet
    with open(os.path.join(outdir, 'style.css'), 'w') as f:
        f.write(styles)

    # render the presentation
    html = md2html.compile_markdown(content)
    content = templ.render(html=html, title=title)

    # save it
    outpath = os.path.join(outdir, 'index.html')
    with open(outpath, 'w') as out:
        out.write(content)

    return outpath
Пример #5
0
def view_source(fnid):
    tagged = db.data[fnid]
    citation = db.sources[fnid]
    tagged = [(md2html.compile_markdown(c), meta)
              for c, meta in tagged.items()]
    return render_template('source.html',
                           fnid=fnid,
                           citation=citation,
                           tagged=tagged)
Пример #6
0
def view_tag(tag):
    tagged, cotags = db.tagged(tag)
    for fnid, d in tagged.items():
        tagged[fnid] = [(md2html.compile_markdown(c), tags) for c, tags in d]
    return render_template('tag.html',
                           tag=tag,
                           tagged=tagged,
                           cotags=cotags,
                           sources=db.sources)
Пример #7
0
def notes(path):
    """Render single or directory of notes for tagging"""
    files = get_files(path)
    if len(files) == 1:
        fns, idx = fn.make_footnotes(files)
        ids = {c: id for id, c in idx.items()}

        docs = []
        for f in files:
            lines = []
            for l in open(f, 'r').read().splitlines():
                if l.startswith('# '):
                    if lines:
                        html = md2html.compile_markdown('\n'.join(lines))
                        docs.append(html)
                        docs.append('</article>')
                        lines = []
                    c = l.strip('# ')
                    docs.append(
                        '<article id="{id}"><a class="fn-ref" id="{id}">{id}</a>'
                        .format(id=ids[c]))
                lines.append(l)
            if lines:
                html = md2html.compile_markdown('\n'.join(lines))
                docs.append(html)
                docs.append('</article>')

        html = '\n'.join(docs)
        tags = {}
        for fnid in idx.keys():
            if fnid in db:
                tags[fnid] = db[fnid]
                for v in tags[fnid].values():
                    v['cite'] = idx[fnid]

        return render_template('notes.html',
                               html=html,
                               tags=json.dumps(tags),
                               fnid_len=config.FNID_LEN)
    else:
        path = os.path.join(config.NOTES_DIR, path)
        files = [(f.replace(config.NOTES_DIR, ''), os.path.relpath(f, path))
                 for f in sorted(files)]
        return render_template('index.html', files=files)
Пример #8
0
def extract_metadata(raw):
    """extract metadata from raw markdown content;
    returns the content with metadata removed and the extracted metadata"""
    # defaults
    meta = {
        'title': '',
        'published_at': datetime.now(),
        'draft': False
    }

    # try to extract metadata, specified as YAML front matter
    meta_match = meta_re.search(raw)
    if meta_match is not None:
        meta_raw = meta_match.group(1)
        try:
            ext_meta = yaml.load(meta_raw)
            meta.update(ext_meta)

            # remove the metadata before we compile
            raw = meta_re.sub('', raw).strip()
        except yaml.scanner.ScannerError:
            pass

    # convert to bool if necessary
    if isinstance(meta['draft'], str):
        meta['draft'] = strtobool(meta['draft'])

    # convert to timestamp if necessary
    if isinstance(meta['published_at'], str):
        meta['published_at'] = parse(meta['published_at'])
    meta['published_ts'] = int(meta['published_at'].timestamp())

    # try to extract title
    title_match = title_re.search(raw)
    if title_match is not None:
        meta['title'] = title_match.group(1)
        meta['title_html'] = compile_markdown(meta['title'])

        # remove the title before we compile
        raw = title_re.sub('', raw).strip()

    return raw, meta
Пример #9
0
def convert():
    """convert markdown from stdin to html"""
    md = sys.stdin.read()
    print(md2html.compile_markdown(md))
Пример #10
0
        fpath = os.path.join(BUILD_DIR, f)
        if os.path.isfile(fpath) or os.path.islink(fpath):
            os.remove(fpath)
        elif os.path.isdir(fpath):
            shutil.rmtree(fpath)

if __name__ == '__main__':
    prep_build_dir()

    # project pages
    for md in glob('projects/*.md'):
        fname = os.path.basename(md)[:-3]
        raw = open(md, 'r').read()
        raw, meta = extract_metadata(raw)
        meta = process_metadata(meta)
        html = compile_markdown(raw)
        templ = env.get_template('project.html')
        html = templ.render(meta=meta, html=html)

        outdir = os.path.join(BUILD_DIR, fname)
        os.mkdir(outdir)
        with open(os.path.join(outdir, 'index.html'), 'w') as f:
            f.write(html)

    # index page
    templ = env.get_template('index.html')
    data = {}
    for k in ['meta', 'projects', 'stalled',
              'channels', 'clients', 'writing']:
        data[k] = yaml.load(open('{}.yaml'.format(k), 'r'))
    html = templ.render(**data)
Пример #11
0
Файл: cli.py Проект: frnsys/nom
def convert():
    """convert markdown from stdin to html"""
    md = sys.stdin.read()
    print(md2html.compile_markdown(md))