Przeglądaj źródła

Add save function

Updates `static/slides.json` (copies backup `archive/`)
master
The Dod 1 rok temu
rodzic
commit
2b39ed5705

+ 1
- 0
.gitignore Wyświetl plik

@@ -1,5 +1,6 @@
1 1
 venv/
2 2
 __pycache__/
3
+archive/*.json
3 4
 work/
4 5
 .env
5 6
 *.swp

+ 28
- 15
app.py Wyświetl plik

@@ -4,28 +4,44 @@ from markdown import markdown
4 4
 from glob import glob
5 5
 import json
6 6
 import os
7
+import sys
8
+import time
9
+import shutil
7 10
 
8 11
 def preprocess_payload(payload):
9
-    for column in payload.get('columns', []):
10
-        md = column.get('markdown')
12
+    for column in payload.get("columns", []):
13
+        md = column.get("markdown")
11 14
         if md:
12
-            column['content'] = markdown(md)
13
-        for slide in column.get('slides', []):
14
-            md = slide.get('markdown')
15
+            column["content"] = markdown(md)
16
+        for slide in column.get("slides", []):
17
+            md = slide.get("markdown")
15 18
             if md:
16
-                slide['content'] = markdown(md)
19
+                slide["content"] = markdown(md)
17 20
 
18 21
 load_dotenv()
19
-payload = json.load(open("static/slides.json"))
20
-preprocess_payload(payload)
21 22
 
22 23
 application = Flask(__name__)
23 24
 
24 25
 @application.get("/")
25 26
 def home():
27
+    payload = json.load(open("static/slides.json"))
28
+    preprocess_payload(payload)
26 29
     return render_template("slides.html", **payload)
27 30
 
28
-@application.get("/choices/<topic>")
31
+@application.post("/update")
32
+def update():
33
+    shutil.copy(
34
+        "static/slides.json",
35
+        time.strftime(
36
+            "archive/slides-%Y-%m-%d-%H.%M.%S.json",
37
+            time.localtime()))
38
+    payload = request.get_json()
39
+    print(type(payload))
40
+    json.dump(payload, sys.stdout, indent=4)
41
+    json.dump(payload, open("static/slides.json", "w"), indent=4)
42
+    return({"status": "success"})
43
+
44
+@application.get("/enum/<topic>")
29 45
 def choices(topic):
30 46
     if topic in ["img", "bg"]:
31 47
         return {"type": "string", "enum": glob("static/{}/*.*".format(topic))}
@@ -35,14 +51,11 @@ def choices(topic):
35 51
 def schema():
36 52
     return render_template("slides.schema.json", backgrounds=choices("bg"))
37 53
 
38
-def home():
39
-    return render_template("slides.html", **payload)
40
-
41 54
 @application.get("/editor")
42 55
 def editor():
43 56
     return render_template("editor.html")
44 57
 
45
-@application.route('/favicon.ico')
58
+@application.route("/favicon.ico")
46 59
 def favicon():
47
-    return send_from_directory(os.path.join(application.root_path, 'static'),
48
-                               'favicon.ico', mimetype='image/vnd.microsoft.icon')
60
+    return send_from_directory(os.path.join(application.root_path, "static"),
61
+                               "favicon.ico", mimetype="image/vnd.microsoft.icon")

+ 1
- 0
archive/README.md Wyświetl plik

@@ -0,0 +1 @@
1
+This is where we keep old versions of `slides.json`

BIN
static/bg/bekir-donmez-stones-4.png Wyświetl plik


BIN
static/bg/bekir-donmez-stones-5.png Wyświetl plik


BIN
static/bg/bekir-donmez-stones-6.png Wyświetl plik


BIN
static/bg/bekir-donmez-stones-7.png Wyświetl plik


BIN
static/bg/bekir-donmez-stones-8.png Wyświetl plik


BIN
static/bg/cat-resting.png Wyświetl plik


+ 1805
- 0
static/bg/twilight-1.svg
Plik diff jest za duży
Wyświetl plik


+ 13
- 12
static/slides.json Wyświetl plik

@@ -1,31 +1,31 @@
1 1
 {
2
-    "title": "Joe's farewell ceremony",
2
+    "title": "My Farewell Ceremony",
3 3
     "columns": [
4 4
         {
5
-            "id": "",
6
-            "title": "Goodbye from George",
5
+            "id": "welcome",
6
+            "title": "Goodbye, mates",
7 7
             "params": {
8
-                "theme": "sky",
9
-                "background-image": "static/img/valley-1.svg",
8
+                "theme": "simple",
9
+                "background-image": "static/bg/valley-1.svg",
10 10
                 "background-opacity": "0.25"
11 11
             },
12
-            "markdown": "This is my **farewell ceremony**.\n\nI [love](https://en.wikipedia.org/wiki/Love) you all."
12
+            "markdown": "Welcome, friends.\n\nWe're about to begin."
13 13
         },
14 14
         {
15 15
             "id": "memory",
16 16
             "title": "How I'd like to be remembered",
17 17
             "params": {
18 18
                 "theme": "solarized",
19
-                "background-image": "static/img/valley-1.svg",
19
+                "background-image": "static/bg/valley-1.svg",
20 20
                 "background-opacity": "0.25"
21 21
             },
22
-            "markdown": "I've done many things. Some of them I'm proud of:",
22
+            "markdown": "I've done many things.\n\nSome of them I'm proud of:",
23 23
             "slides": [
24 24
                 {
25 25
                     "id": "work",
26 26
                     "title": "My work",
27 27
                     "params": {
28
-                        "background-image": "static/img/valley-1.svg",
28
+                        "background-image": "static/bg/valley-1.svg",
29 29
                         "background-opacity": "0.25"
30 30
                     },
31 31
                     "markdown": "### I've worked like a horse.\n\n![A horse (of course)|w-50 centered](static/img/horse.png)"
@@ -35,7 +35,7 @@
35 35
                     "title": "The rest",
36 36
                     "params": {
37 37
                         "theme": "moon",
38
-                        "background-image": "static/img/particles-1.png",
38
+                        "background-image": "static/bg/twilight-1.svg",
39 39
                         "background-size": "contain"
40 40
                     },
41 41
                     "markdown": "#### I've rested like a cat.\n\n![A cat resting|w-66 centered](static/img/cat-resting.png)"
@@ -47,7 +47,7 @@
47 47
             "title": "Who I seek forgiveness from",
48 48
             "params": {
49 49
                 "theme": "league",
50
-                "background-image": "static/img/particles-1.svg",
50
+                "background-image": "static/bg/particles-1.svg",
51 51
                 "background-opacity": "0.25"
52 52
             },
53 53
             "markdown": "I've made many mistakes.\n\nI hope you can forgive me:",
@@ -55,6 +55,7 @@
55 55
                 {
56 56
                     "id": "horse",
57 57
                     "title": "My horse",
58
+                    "params": {},
58 59
                     "markdown": "![My horse|w-50 float-right](static/img/horse2.png)\n\nPlease forgive me, horse, for this and that."
59 60
                 },
60 61
                 {
@@ -68,4 +69,4 @@
68 69
             ]
69 70
         }
70 71
     ]
71
-}
72
+}

+ 29
- 5
static/slides.schema.json Wyświetl plik

@@ -7,10 +7,13 @@
7 7
     },
8 8
     "columns": {
9 9
       "type": "array",
10
-      "title": "Columns",
10
+      "title": "Chapters",
11
+      "options": {
12
+        "collapsed": "true"
13
+      },
11 14
       "items": {
12 15
         "type": "object",
13
-        "title": "Column",
16
+        "title": "Chapter",
14 17
         "properties": {
15 18
           "id": {
16 19
             "type": "string"
@@ -26,12 +29,13 @@
26 29
           },
27 30
           "slides": {
28 31
             "type": "array",
32
+            "title": "Pages",
29 33
             "options": {
30 34
               "collapsed": "true"
31 35
             },
32 36
             "items": {
33 37
               "type": "object",
34
-              "title": "Slide",
38
+              "title": "Page",
35 39
               "properties": {
36 40
                 "id": {
37 41
                   "type": "string"
@@ -89,7 +93,7 @@
89 93
           "default": "simple"
90 94
         },
91 95
         "background-image": {
92
-          "$ref": "/choices/bg",
96
+          "$ref": "/enum/bg",
93 97
           "title": "Background image",
94 98
           "links": [
95 99
             {
@@ -124,7 +128,27 @@
124 128
     "markdown": {
125 129
       "type": "string",
126 130
       "title": "Content",
127
-      "format": "markdown"
131
+      "format": "markdown",
132
+      "options": {
133
+        "simplemde": {
134
+          "renderingConfig": {
135
+            "singleLineBreaks": false
136
+          },
137
+          "toolbar": [
138
+            "bold",
139
+            "italic",
140
+            "heading",
141
+            "|",
142
+            "ordered-list",
143
+            "unordered-list",
144
+            "|",
145
+            "link",
146
+            "quote",
147
+            "|",
148
+            "preview"
149
+          ]
150
+        }
151
+      }
128 152
     }
129 153
   }
130 154
 }

+ 11
- 4
templates/editor.html Wyświetl plik

@@ -16,7 +16,10 @@
16 16
     <div class="container">
17 17
       <h1>Presentation editor</h1>
18 18
       <div id='editor_holder'></div>
19
-      <button class="btn btn-success" id='submit'>Submit (console.log)</button>
19
+      <div class="btn-group mt-2">
20
+        <button class="btn btn-success" id='submit'>Submit</button>
21
+        <a class="btn btn-secondary" href="/">Cancel</a>
22
+      </div>
20 23
     </div>
21 24
     <script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
22 25
     <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
@@ -25,11 +28,9 @@
25 28
       fetch("/static/slides.schema.json")
26 29
         .then(res => res.json())
27 30
         .then(schema => {
28
-          console.log(schema);
29 31
           fetch("/static/slides.json")
30 32
             .then(res => res.json())
31 33
             .then(slides => {
32
-              console.log(slides);
33 34
               // Initialize the editor with a JSON schema
34 35
               var editor = new JSONEditor(
35 36
                 document.getElementById('editor_holder'), {
@@ -42,7 +43,13 @@
42 43
               // Hook up the submit button to log to the console
43 44
               document.getElementById('submit').addEventListener('click',() => {
44 45
                 // Get the value from the editor
45
-                console.log(editor.getValue());
46
+                fetch("/update", {
47
+                  method: "POST",
48
+                  body: JSON.stringify(editor.getValue()),
49
+                  headers: {
50
+                      "Content-type": "application/json; charset=UTF-8"
51
+                  }
52
+                }).then((response) => { location.replace('/') });
46 53
               });
47 54
             });
48 55
           });

+ 7
- 7
templates/slides.html Wyświetl plik

@@ -23,24 +23,24 @@
23 23
 	</head>
24 24
 	<body>
25 25
                 <div id="custom-nav">
26
-                  <a href="#/" title="Home">
27 26
                   <!-- the zero-width-space is a tweak against tidy removing empty tags -->
28
-                   <i class="fa fa-home">​</i></a> <a id="top-link" title="Section top"
29
-	                  disabled="disabled"><i class="fa fa-angle-double-up">​</i></a>
27
+                  <a href="#/" title="Home"><i class="fa fa-home">​</i></a>
28
+                  <a id="top-link" title="Section top" disabled="disabled"><i class="fa fa-angle-double-up">​</i></a>
29
+                  <a href="/editor" title="Edit"><i class="fa fa-user-edit">​</i></a>
30 30
                 </div>
31 31
 		<div class="reveal">
32 32
 			<div class="slides">
33 33
                           {% for column in columns %}
34 34
 				<section{% if column.params %}{% for key, value in column.params|dictsort %} data-{{key}}="{{value}}"{% endfor %}{% endif %}>
35
-                                    <section id="{{column.id|default('home')}}">
35
+                                    <section id="{{column.id}}">
36 36
                                         {# column's top slide #}
37 37
                                         <h3 class="title slide_title">{{column.title}}</h3>
38 38
                                         {% if column.content %}{{ column.content|safe }}{% endif %}
39
-                                        {# Main column (no id) shows links to other columns #}
40
-                                        {% if not column.id %}
39
+                                        {# Main column shows links to other columns #}
40
+                                        {% if loop.first %}
41 41
                                             <ul>
42 42
                                                 {% for other in columns %}
43
-                                                    {% if other.id %}<li><a href="#/{{other.id}}">{{other.title}}</a></li>{% endif %}
43
+                                                    {% if not loop.first %}<li><a href="#/{{other.id}}">{{other.title}}</a></li>{% endif %}
44 44
                                                 {% endfor %}
45 45
                                             </ul>
46 46
                                         {% endif %}

Ładowanie…
Anuluj
Zapisz