aboutsummaryrefslogtreecommitdiff
path: root/wikijscmd/commands.py
blob: ae905074d8789b75f97cc4886d137abf0ed99de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import sys
import subprocess
import time
import os

from wikijscmd import graphql_queries
from datetime import datetime, timedelta
from wikijscmd.util import clean_filename, get_tree, open_editor, get_single_page, print_item, args_for_date

def create(path, title, content=None):
    page = get_single_page(path)
    if page is not None:
        print("Page already exists with path: %s" % path)
        if input("Edit it? (y/n) ") == "y":
            edit(path)
            return
    if not content:
        content = open_editor("create", path, "")
    response = graphql_queries.create_page(content, title, path)
    result = response["data"]["pages"]["create"]["responseResult"]
    if not result["succeeded"]:
        print("Error!", result["message"])
        sys.exit(1)
    print(result["message"])

def tree(regex):
    """
    Finds pages based on a path search
    """
    for item in get_tree(regex):
        print_item(item)

def single(path, raw=False):
    """
    View a page with the given path
    """
    page = get_single_page(path)
    if page is None:
        print("No page with path: %s" % path)
        sys.exit(1)
    if raw:
        print("-" * 80)
        print_item(page)
        print("-" * 80)
    print(page["content"])

def move(source, dest):
    """
    Move a page from one path to another
    """
    page = get_single_page(source)
    if page is None:
        print("Source page %s does not exist" % source)
        sys.exit(1)
    response = graphql_queries.move_page(page["id"], dest)
    result = response["data"]["pages"]["move"]["responseResult"]
    if not result["succeeded"]:
        print("Error!", result["message"])
        sys.exit(1)
    print(result["message"])

def edit(path, save=False):
    """
    Edit a page
    """
    page = get_single_page(path)
    if page is None:
        print("No page with path: %s" % path)
        if input("Create it? (y/n) ") == "y":
            title = input("Enter the title: ").strip()
            create(path, title)
        return
    body = page["content"]

    # Open it in editor
    new_body = open_editor("edit", path, body)

    # Prompt user to save it to the wiki
    print_item(page)
    print("-" * 80)
    print(new_body)
    print("-" * 80)
    if save or input("Save changes? (y/n) ") == "y":
        response = graphql_queries.edit_page(page["id"], new_body, page["title"], page["path"])
        result = response["data"]["pages"]["update"]["responseResult"]
        if not result["succeeded"]:
            print("Error!", result["message"])
            sys.exit(1)
        print(result["message"])

def latest_journal_entry(print_result=True):
    last_date = None
    for page in get_tree("journal"):
        try:
            date = datetime.strptime(page["path"], "journal/%Y/%b/%d")
            if last_date is None or date > last_date:
                last_date = date
        except ValueError:
            continue
    if print_result:
        print(last_date)
    return last_date

def fill_in_pages():
    last_date = latest_journal_entry(print_result=False)
    today = datetime.now().date()
    if last_date is None:
        last_date = today
    pending_date = last_date.date()
    while pending_date < today:
        pending_date += timedelta(days=1)
        create(**args_for_date(pending_date))

def today():
    """
    Creates a journal page with the path "journal/YYYY/MM/DD"
    """
    args = args_for_date(datetime.now().date())
    if get_single_page(args["path"]) is not None:
        edit(args["path"])
    else:
        create(**args)

def view(path):
    """
    Opens a journal page in the defaul program
    """
    page = get_single_page(path)
    if page is None:
        print("No page with path: %s" % path)
        sys.exit(1)
    filename = "/tmp/wikijscmd-"+clean_filename(path)+".md"
    out_filename = "/tmp/wikijscmd-"+clean_filename(path)+".html"
    with open(filename, "w") as f:
        f.write(page["content"])
    subprocess.run(["pandoc", filename, "-o", out_filename])
    subprocess.run(["xdg-open", out_filename])
    time.sleep(5)
    os.remove(filename)
    os.remove(out_filename)