aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2020-12-28 21:36:02 -0600
committerMark Powers <mark@marks.kitchen>2020-12-28 21:36:02 -0600
commit5745b14719e7c16c12d85b60adafd6ea0335731e (patch)
tree9c187d765a4f07e4852a34bcc69c9b58bab7a309
parent2fe62e6f232ea88b552d9bec295252e330e899d4 (diff)
Prompt edit/create if page already exists/does not exist.
-rw-r--r--README.md9
-rwxr-xr-xmain.py33
2 files changed, 31 insertions, 11 deletions
diff --git a/README.md b/README.md
index 4756c27..990355a 100644
--- a/README.md
+++ b/README.md
@@ -21,8 +21,8 @@ prints the page contents for the given path or ID parameters. An ID is the
integer type for the page. Path may optionally start with a / to indicate that
an integer only path is not an ID.
-### tree PATH\_CONTAINS?
-prints out the tree of all pages where the path contains the given parameter.
+### tree PATH\_REGEX?
+prints out the tree of all pages where the path matches the regex given.
Printed information includes the page title, ID, path.
### today
@@ -51,3 +51,8 @@ example, if your wiki is at `wiki.example.com`, the url field should be set to
Run `main.py` in order to use the program.
+
+## TODO
+- Include options to always answer yes or no
+- Allow for moving pages
+
diff --git a/main.py b/main.py
index 168c7bf..9a0a51e 100755
--- a/main.py
+++ b/main.py
@@ -16,15 +16,24 @@ def today(argv):
print("Usage: ./main.py today")
today = datetime.datetime.now()
path = today.strftime("%Y/%b/%d").lower()
- date_int = int(today.strftime("%d"))
- title = today.strftime("%B ") + str(date_int)
- create([path, title])
+ if get_single_page([path]) is not None:
+ edit([path])
+ else:
+ date_int = int(today.strftime("%d"))
+ title = today.strftime("%B ") + str(date_int)
+ create([path, title])
def create(argv):
if len(argv) < 2 or len(argv) > 3:
print("Usage: ./main.py create <path> <title> <content?>")
sys.exit(1)
path = argv[0]
+ page = get_single_page(argv)
+ if page is not None:
+ print("Page already exists with path: %s" % argv[0])
+ if input("Edit it? (y/n) ") == "y":
+ edit([path])
+ return
title = argv[1]
if len(argv) == 2:
content = open_editor("create", path, "")
@@ -40,7 +49,7 @@ def create(argv):
def tree(argv):
response = graphql_queries.get_tree()
for item in response["data"]["pages"]["list"]:
- if len(argv) == 1 and argv[0] not in item["path"]:
+ if len(argv) == 1 and not re.search(argv[0], item["path"]):
continue
print_item(item)
@@ -56,8 +65,7 @@ def get_single_page(argv):
argument = item["id"]
found = True
if not found:
- print("No page with path: %s" % argument)
- sys.exit(0)
+ return None
page_id = int(argument)
response = graphql_queries.get_single_page(page_id)
return response["data"]["pages"]["single"]
@@ -67,6 +75,8 @@ def single(argv):
print("Usage: ./main.py single <id|path>")
sys.exit(0)
page = get_single_page(argv)
+ if page is None:
+ print("No page with path: %s" % argument)
print("-" * 80)
print_item(page)
print("-" * 80)
@@ -96,12 +106,17 @@ def edit(argv):
# Load content to edit
if len(argv) != 1:
print("Usage: ./main.py edit <id|path>")
- sys.exit(0)
+ sys.exit(1)
page = get_single_page(argv)
+ if page is None:
+ print("No page with path: %s" % argv[0])
+ if input("Create it? (y/n) ") == "y":
+ title = input("Enter the title: ").strip()
+ create([argv[0], title])
+ return
body = page["content"]
# Open it in editor
- print(argv)
new_body = open_editor("edit", argv[0], body)
# Prompt user to save it to the wiki
@@ -122,7 +137,7 @@ def main():
print("Usage: ./main.py <command> <args>")
print("Commands:")
print("\tcreate <path> <title> <content?>")
- print("\ttree <contains?>")
+ print("\ttree <regex?>")
print("\tsingle <id|path>")
print("\tedit <id|path>")
print("\ttoday")