Exemplo n.º 1
0
def test_page_non_existent(tmp_root: TemporaryDirectory):
    page = Page("test1", root=tmp_root.name)
    assert page.root == tmp_root.name
    assert page.path == pjoin(tmp_root.name, "test1.md")
    assert page.relpath == "test1.md"
    assert not exists(page.path)
    page.save()
    assert exists(page.path)
Exemplo n.º 2
0
def save(path):  # noqa: D103
    if not request.is_json:
        return 401
    markdown = request.json["markdown"]
    page_to_save = Page(path, CONTENT_ROOT)
    page_to_save.markdown = markdown
    page_to_save.save()
    RECENT_FILES.update(page_to_save.path)
    return "OK", 201
Exemplo n.º 3
0
def edit(path):  # noqa: D103
    # Nooooon rien de rien....
    return render_template(
        "edit.html.j2",
        favicon=FAVICON_PATH,
        title_prefix=TITLE_PREFIX,
        page=Page(path, CONTENT_ROOT),
        recent=(Page(f["path"], CONTENT_ROOT) for f in RECENT_FILES.get(9)),
    )
Exemplo n.º 4
0
def test_page_title(tmp_root: TemporaryDirectory):
    page = Page("test_title", root=tmp_root.name)
    assert page.title == "test_title"
    page.markdown = """# This is a title
    
    And this, content.
    """
    assert page.title == "This is a title"
    page.markdown = "Title: This is an other title\n\n" + page.markdown
    assert page.title == "This is an other title"
Exemplo n.º 5
0
def page(path):  # noqa: D103
    if (not str(path).endswith("/") and exists(pjoin(CONTENT_ROOT, path))
            and not isdir(pjoin(CONTENT_ROOT, path))):
        return send_from_directory(pjoin(CONTENT_ROOT, dirname(path)),
                                   basename(path))
    if str(path).endswith("/"):
        return redirect(path[:-1])
    page_to_view = Page(path, root=CONTENT_ROOT, shallow=False)
    if page_to_view.markdown == "":
        return redirect(path + "/edit")
    return render_template(
        "page.html.j2",
        favicon=FAVICON_PATH,
        title_prefix=TITLE_PREFIX,
        page=page_to_view,
        recent=(Page(f["path"], root=CONTENT_ROOT)
                for f in RECENT_FILES.get(9)),
    )
Exemplo n.º 6
0
def test_existing_page(tmp_root: TemporaryDirectory):
    with open(pjoin(tmp_root.name, "existing.md"), "w+") as f:
        f.write("""# Sample content
            
            Yeah, just a couple lines.
            """)
    assert exists(pjoin(tmp_root.name, "existing.md"))
    page = Page("existing.md", root=tmp_root.name)
    assert page.root == tmp_root.name
    assert page.path == pjoin(tmp_root.name, "existing.md")
    assert page.markdown != ""
    assert "# Sample content" in page.markdown
Exemplo n.º 7
0
def delete(path):  # noqa: D103
    p = Page(path, CONTENT_ROOT)
    try:
        os.remove(p.path)
        RECENT_FILES.delete(p.path)
        if repository is not None:
            logger.info("Deleting page %s from git", p.title)
            repository.index.add([p.path])
            repository.index.commit(message="Delete {}".format(p.path))
        return "OK", 201
    except OSError as e:
        if repository is not None:
            repository.index.remove(p.path)
        return "Could not delete page: " + str(e), 404
Exemplo n.º 8
0
def write_todo_to_journal(basepath: str, todo: dict):
    """
    Write the object to a Page, denoted as journal in the URL.

    The given item should have the following keys: 'id', 'text'
    """
    p = Page(basepath, date.today().strftime("journal/%Y/%m/%d"))
    # load existing
    p.load()
    if p.markdown == "":
        # we are freeeee
        p.markdown = """# journal du {d}

## Done

* {id}: {text}
""".format(
            d=date.today().strftime("%Y/%m/%d"), **todo
        )
        p.save()
        return
    match = re.match(r"#+ *Done\n+", p.markdown)
    if not match:
        p.markdown = p.markdown + "\n\n## Done\n\n"
    p.markdown = p.markdown + "* {id}: {text}\n".format(**todo)
    p.save()