diff --git a/redstring/parser.py b/redstring/parser.py index 87c9ae6..1ccbfed 100644 --- a/redstring/parser.py +++ b/redstring/parser.py @@ -63,13 +63,7 @@ class TagOptions: _PRIVATE_KEY = 'private' def __init__(self, **kwargs) -> None: - self.options = OrderedDict(**kwargs) - # Tag value is a hyperlink - self.hyperlink: bool = kwargs.get('hyperlink', False) - # Tag value contains redstring interlinks - self.interlink: bool = kwargs.get('interlink', False) - # Hide the tag from unauthenticated viewers - self.private: bool = kwargs.get('private', False) + self.options: dict = OrderedDict(**kwargs) def to_json(self): return self.options diff --git a/redstring/server.py b/redstring/server.py index 0dd0d4a..116a6de 100644 --- a/redstring/server.py +++ b/redstring/server.py @@ -10,12 +10,13 @@ from flask import ( Flask, redirect, render_template, + request, safe_join, url_for, ) from redstring.library import generate_index_document, generate_default_document -from redstring.parser import load, dump +from redstring.parser import load, dump, DocumentTab, DocumentTag, TagOptions, DocumentSubtag CONFIG_ENVVAR = 'REDSTRING_CONFIG' @@ -54,6 +55,47 @@ def new(): return redirect(url_for('document', document_id=document_id)) +@app.route('/edit/', methods=['GET', 'POST']) +def edit(document_id): + # Load the document to edit + doc_path = safe_join(current_app.config['root'], f'{document_id}.json') + if not os.path.isfile(doc_path): + return abort(404) + with open(doc_path) as f: + doc = load(f) + + # Check for structural change requests + if add := request.args.get('add'): + if add == 'tab': + new_tab = DocumentTab('newtab', []) + doc.tabs.append(new_tab) + with open(doc_path, 'w') as f: + dump(doc, f) + return redirect(url_for('edit', document_id=document_id)) + elif add == 'tag': + if tab_name := request.args.get('tab'): + tab = doc.get_tab(tab_name) + new_tag = DocumentTag('tag', '', TagOptions(private=True)) + tab.tags.append(new_tag) + with open(doc_path, 'w') as f: + dump(doc, f) + return redirect(url_for('edit', document_id=document_id)) + return abort(400) + elif add == 'subtag': + if tag_name := request.args.get('tag'): + tag = doc.get_tag(tag_name) + new_subtag = DocumentSubtag('subtag', '', TagOptions(private=True)) + tag.subtags.append(new_subtag) + with open(doc_path, 'w') as f: + dump(doc, f) + return redirect(url_for('edit', document_id=document_id)) + return abort(400) + + # Otherwise, return the editor page + else: + return render_template('edit.jinja', document=doc, index=False) + + def main(): parser = argparse.ArgumentParser(description="Run the redstring server.") parser.add_argument("--config", help="Config file path.") diff --git a/redstring/templates/base.jinja b/redstring/templates/base.jinja index b2729a5..c55243b 100644 --- a/redstring/templates/base.jinja +++ b/redstring/templates/base.jinja @@ -101,12 +101,20 @@ table.page-table td:nth-child(2) { white-space: pre-wrap; } - table.page-table td:nth-child(2) a { + table.page-table a { color: #8af; } - table.page-table td:nth-child(2) a:visited { + table.page-table a:visited { color: #88f; } + + /* Edit page styling */ + input.tag-name { + } + textarea.tag-value { + resize: none; + width: 100%; + } {% block page_scripts %}{% endblock %} diff --git a/redstring/templates/edit.jinja b/redstring/templates/edit.jinja new file mode 100644 index 0000000..4044693 --- /dev/null +++ b/redstring/templates/edit.jinja @@ -0,0 +1,135 @@ +{% extends 'base.jinja' %} + +{% set page_title = document.get_tag_value('title', document.get_tag('id').value) -%} +{% set page_summary = document.get_tag_value('summary', '') %} + +{% block page_scripts %} + +{% endblock page_scripts %} + +{% macro make_content_tab(tab, selected) -%} +
{{ tab.name }}
+{%- endmacro %} + +{% macro make_tab_page(tab, selected) %} +
+ + + + + + + +{% for tag in tab.tags %} + +{%- if tag.name == 'id' -%} + +{%- else -%} + +{% endif %} + + + + + + + + +{% for subtag in tag.subtags %} + + + + + + + + + +{% endfor %} + +{% endfor %} + + + + + + +
tab name
{{ tab.name }}
{{ tag.name }}
{{ tag.name }}
{{ tag.value }}
+{%- if tag.subtags -%} +│ +{%- else -%} +└ + +{%- endif -%}
{{ subtag.name }}
{{ subtag.value }}
+{%- if not loop.last -%} +│ +{%- else -%} +└ + +{%- endif -%} +
Add tag
+
+{% endmacro %} + +{# TODO: tab.priority support #} +{% block page_content %} +
+{%- for tab in document -%} +{{ make_content_tab(tab, loop.first) }} +{%- endfor -%} + + +
+{% for tab in document -%} +{{ make_tab_page(tab, loop.first) }} +{%- endfor -%} +{% endblock page_content %} \ No newline at end of file