-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmsimfly.leo
2945 lines (2705 loc) · 130 KB
/
cmsimfly.leo
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
<globals body_outline_ratio="0.5" body_secondary_ratio="0.5">
<global_window_position top="50" left="50" height="500" width="700"/>
<global_log_window_position top="0" left="0" height="0" width="0"/>
</globals>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="amd.20160714225308.1"><vh>@settings</vh>
<v t="amd.20160714225308.2"><vh>@data qt-gui-plugin-style-sheet</vh></v>
<v t="amd.20160714225308.3"><vh>@string initial_split_orientation = horizontal</vh></v>
</v>
<v t="leo.20180702145534.1"><vh>wsgi start/stop</vh>
<v t="leo.20180702142719.1"><vh>@button start wsgi</vh></v>
<v t="leo.20180702142849.1"><vh>@button stop wsgi</vh></v>
</v>
<v t="leo.20180702145555.1"><vh>http start/stop</vh>
<v t="leo.20180702145442.1"><vh>@button start http.server</vh></v>
<v t="leo.20180702145458.1"><vh>@button stop http.server</vh></v>
</v>
<v t="amd.20160714225245.2"><vh>about cmsimfly</vh>
<v t="amd.20160714225847.1"><vh>@edit README.md</vh></v>
<v t="amd.20160714230137.1"><vh>@edit .gitignore</vh></v>
<v t="amd.20160714230155.1"><vh>@edit LICENSE</vh></v>
<v t="amd.20160715000709.1"><vh>註解</vh>
<v t="amd.20160715211540.1"><vh>處理 url 與 edit</vh></v>
<v t="amd.20160715213810.1"><vh>必須處理 downloads 與 images 目錄</vh></v>
<v t="leo.20180625155103.1"><vh>改為 pybean based</vh>
<v t="leo.20180625155310.1"><vh>資料庫設計</vh></v>
</v>
<v t="leo.20180630135634.1"><vh>新增 generate_pages 功能</vh>
<v t="leo.20180630211407.1"><vh>必須修改頁面內容</vh></v>
</v>
</v>
<v t="leo.20180701102916.1"><vh>2018Summer 改版</vh>
<v t="leo.20180702104314.1"><vh>整合設定檔案</vh></v>
<v t="leo.20180701205822.1"><vh>editorhead 修改</vh></v>
<v t="leo.20180701212101.1"><vh>有關 Cango library</vh></v>
<v t="leo.20180726214539.1"><vh>存檔時移除所有 tag of title</vh></v>
<v t="leo.20180731143510.1"><vh>重大 bug</vh></v>
</v>
<v t="leo.20181005174347.1"><vh>2018Fall 改版</vh></v>
<v t="leo.20180915084539.1"><vh>20180915</vh></v>
<v t="leo.20190104175029.1"><vh>20190104</vh></v>
<v t="amd.20160718002238.1"><vh>參考資料</vh></v>
<v t="amd.20160717220157.1"><vh>Flask 開講</vh></v>
</v>
<v t="leo.20180701102750.1"><vh>@edit index.html</vh></v>
<v t="amd.20160714225914.1"><vh>@edit setup.py</vh></v>
<v t="amd.20160714230021.1"><vh>@edit init.py</vh></v>
<v t="amd.20160714230050.1"><vh>@edit nocache.py</vh></v>
<v t="leo.20180625155248.1"><vh>@edit pybean.py</vh></v>
<v t="leo.20180625155728.1"><vh>@edit schema.sql</vh></v>
<v t="amd.20160718130611.1"><vh>@edit .gitignore</vh></v>
<v t="amd.20160717222221.1"><vh>@path static</vh>
<v t="amd.20160717222242.1"><vh>@edit axuploader.js</vh></v>
<v t="amd.20160718125932.1"><vh>@edit viewstl.html</vh></v>
<v t="amd.20160718125943.1"><vh>@edit stlviewer.js</vh></v>
<v t="amd.20160718212341.1"><vh>@edit jsc3d.js</vh></v>
</v>
<v t="amd.20160714230058.1"><vh>@clean wsgi.py</vh>
<v t="amd.20160714230122.1"><vh>wsgi declarations</vh></v>
</v>
<v t="amd.20160714230833.1" a="E"><vh>編輯 flaskapp.py</vh>
<v t="amd.20160714230833.2" a="E"><vh>@clean flaskapp.py</vh>
<v t="amd.20160714230833.3"><vh><<declarations>></vh></v>
<v t="amd.20160714230833.4"><vh><<registers>></vh></v>
<v t="amd.20160714233131.1"><vh>checkLogin</vh></v>
<v t="amd.20160717235504.1"><vh>delete_file</vh></v>
<v t="amd.20160715161158.1"><vh>doDelete</vh></v>
<v t="amd.20160715161508.1"><vh>doSearch</vh></v>
<v t="amd.20160714230833.16"><vh>download</vh></v>
<v t="amd.20160715160505.1"><vh>download_list</vh></v>
<v t="amd.20160714231903.1"><vh>downloadlist_access_list</vh></v>
<v t="amd.20160718212924.1"><vh>downloads</vh></v>
<v t="amd.20160714232140.1"><vh>downloadselect_access_list</vh></v>
<v t="amd.20160715161642.1"><vh>edit_config</vh></v>
<v t="amd.20160714233320.1"><vh>edit_page</vh></v>
<v t="amd.20160715000858.1"><vh>editorfoot</vh></v>
<v t="amd.20160714231555.1"><vh>editorhead (#4)</vh></v>
<v t="amd.20160715154211.1"><vh>error_log</vh></v>
<v t="amd.20160714231953.1"><vh>file_get_contents</vh></v>
<v t="amd.20160714231640.1"><vh>file_lister</vh></v>
<v t="amd.20160715160325.1"><vh>file_selector (#4)</vh></v>
<v t="amd.20160714231628.1"><vh>file_selector_script (#4)</vh></v>
<v t="amd.20160715155334.1"><vh>fileaxupload</vh></v>
<v t="amd.20160715154923.1"><vh>fileuploadform</vh></v>
<v t="amd.20160715155703.1"><vh>flvplayer</vh></v>
<v t="leo.20180630142017.1"><vh>generate_pages</vh></v>
<v t="amd.20160714231055.1"><vh>get_page</vh></v>
<v t="leo.20180630144325.1"><vh>get_page2</vh></v>
<v t="amd.20160715161018.1"><vh>image_delete_file</vh></v>
<v t="amd.20160715161251.1"><vh>image_doDelete</vh></v>
<v t="amd.20160715160642.1"><vh>image_list</vh></v>
<v t="amd.20160715160036.1"><vh>imageaxupload</vh></v>
<v t="amd.20160714231915.1"><vh>imagelist_access_list</vh></v>
<v t="amd.20160714232205.1"><vh>imageselect_access_list</vh></v>
<v t="amd.20160715155918.1"><vh>imageuploadform</vh></v>
<v t="amd.20160715152825.1"><vh>index</vh></v>
<v t="amd.20160714232513.1"><vh>isAdmin</vh></v>
<v t="amd.20160715161923.1"><vh>listdir</vh></v>
<v t="amd.20160715160836.1"><vh>load_list</vh></v>
<v t="amd.20160714232156.1"><vh>loadlist_access_list</vh></v>
<v t="amd.20160714232921.1"><vh>login</vh></v>
<v t="amd.20160715154321.1"><vh>logout</vh></v>
<v t="amd.20160714231619.1"><vh>parse_config</vh></v>
<v t="leo.20181002215447.1"><vh>_remove_h123_attrs</vh></v>
<v t="leo.20181002215437.1"><vh>parse_content</vh></v>
<v t="amd.20160714231359.1"><vh>render_menu</vh></v>
<v t="leo.20180630203542.1"><vh>render_menu2</vh></v>
<v t="amd.20160715161730.1"><vh>saveConfig</vh></v>
<v t="amd.20160715154534.1"><vh>savePage</vh></v>
<v t="amd.20160714231440.1"><vh>search_content</vh></v>
<v t="amd.20160715161352.1"><vh>search_form</vh></v>
<v t="amd.20160714230833.20"><vh>send_file</vh></v>
<v t="amd.20160714230833.17"><vh>send_images</vh></v>
<v t="amd.20160714230833.18"><vh>send_static</vh></v>
<v t="amd.20160714231934.1"><vh>set_admin_css</vh></v>
<v t="amd.20160714232359.1"><vh>set_css</vh></v>
<v t="leo.20180630203825.1"><vh>set_css2</vh></v>
<v t="amd.20160714231944.1"><vh>set_footer</vh></v>
<v t="amd.20160715162003.1"><vh>sitemap</vh></v>
<v t="leo.20180630203449.1"><vh>sitemap2</vh></v>
<v t="amd.20160714231925.1"><vh>sizeof_fmt</vh></v>
<v t="amd.20160715154701.1"><vh>ssavePage</vh></v>
<v t="amd.20160714232059.1"><vh>syntaxhighlight</vh></v>
<v t="leo.20180630233136.1"><vh>syntaxhighlight2</vh></v>
<v t="amd.20160714231606.1"><vh>tinymce_editor (#4)</vh></v>
<v t="amd.20160714232232.1"><vh>unique</vh></v>
</v>
</v>
<v t="leo.20180801114529.1"><vh>trash (20180801)</vh>
<v t="leo.20180801114537.1"><vh>parse_content_old</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="amd.20160714225245.2">we are going to use flask to write an simple cms system based upon cmsimply.
</t>
<t tx="amd.20160714225308.1"></t>
<t tx="amd.20160714225308.2">QSplitter::handle {
background-color: #CAE1FF; /* lightSteelBlue1 */
}
QStackedWidget {
/* background-color:lightpink; */
border-color: red;
padding: 0px;
/* border-width: 0px; */
/* background-color: yellow; */
}
QSplitter {
border-color: white;
background-color: white;
border-style: solid;
}
QTreeWidget {
/* These apply to the selected item, but not to editing items.*/
background-color: #ffffec; /* Leo's traditional tree color */
selection-color: black; /* was white */
selection-background-color: lightgrey;
/* font-family: SansSerif; */
/*font-family: DejaVu Sans Mono;*/
font-family:YaHei Mono;
/* 標題字型大小設定 */
font-size: 22px;
font-weight: normal; /* normal,bold,100,..,900 */
font-style: normal; /* normal, italic,oblique */
}
/* Headline edit widgets */
QTreeWidget QLineEdit {
background-color: cornsilk;
selection-color: white;
selection-background-color: blue;
/*font-family: DejaVu Sans Mono;*/
font-family:YaHei Mono;
/* 沒有特別對應字型大小 */
font-size: 22px;
font-weight: normal; /* normal,bold,100,..,900 */
font-style: normal; /* normal, italic,oblique */
}
/* The log panes */
QTextEdit {
background-color: #f2fdff;
selection-color: red;
selection-background-color: blue;
/* font-family: Courier New; */
font-family:YaHei Mono;
/* log font 大小 */
font-size: 22px;
font-weight: normal; /* normal,bold,100,..,900 */
font-style: normal; /* normal, italic,oblique */
}
/* The body pane */
QTextEdit#richTextEdit {
background-color: #fdf5f5; /* A kind of pink. */
selection-color: white;
selection-background-color: red;
/*font-family: DejaVu Sans Mono;*/
/* font-family: Courier New; */
font-family:YaHei Mono;
/* 內文字型大小 */
font-size: 22px;
font-weight: normal; /* normal,bold,100,..,900 */
font-style: normal; /* normal,italic,oblique */
}
QLabel {
font-family:YaHei Mono;
/* 下方的 Minibuffer 標題字型大小 */
font-size: 22px;
font-weight: normal; /* normal,bold,100,..,900 */
font-style: normal; /* normal,italic,oblique */
}
/* Editor labels */
QLineEdit#editorLabel {
background-color: #ffffec;
font-family:YaHei Mono;
/* 沒有直接對應字型大小 */
font-size: 22px;
font-weight: normal; /* normal,bold,100,..,900 */
font-style: normal; /* normal,italic,oblique */
border: 2px;
margin: 2px;
}</t>
<t tx="amd.20160714225308.3">horizontal: body pane to the left
vertical: body pane on the botton</t>
<t tx="amd.20160714230058.1">@language python
@tabwidth -4
@others
</t>
<t tx="amd.20160714230122.1">#!/usr/bin/python
# 導入 os 模組, 主要用來判斷是否以 uwsgi 或一般近端模式執行
import os
# 導入同目錄下的 flaskapp.py
import flaskapp
import ssl
# 即使在近端仍希望以 https 模式下執行
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain('localhost.crt', 'localhost.key')
# 取 flaskapp.py 中的 uwsgi 變數設定
uwsgi = flaskapp.uwsgi
if uwsgi:
# 表示程式在雲端執行
application = flaskapp.app
else:
# 表示在近端執行, 以 python3 wsgi.py 執行
flaskapp.app.run(host='127.0.0.1', port=9443, debug=True, ssl_context=context)
</t>
<t tx="amd.20160714230833.1"></t>
<t tx="amd.20160714230833.16">@app.route('/download/', methods=['GET'])
def download():
"""Download file using URL."""
filename = request.args.get('filename')
type = request.args.get('type')
if type == "files":
return send_from_directory(download_dir, filename=filename)
else:
# for image files
return send_from_directory(image_dir, filename=filename)
</t>
<t tx="amd.20160714230833.17"># setup static directory
#@app.route('/images/<path:path>')
@app.route('/images/<path:path>')
def send_images(path):
"""send image files"""
return send_from_directory(_curdir + "/images/", path)
</t>
<t tx="amd.20160714230833.18"># setup static directory
@app.route('/static/')
def send_static():
"""send static files"""
return app.send_static_file('index.html')
</t>
<t tx="amd.20160714230833.2">@language python
@tabwidth -4
<<declarations>>
<<registers>>
@others
if __name__ == "__main__":
app.run()
</t>
<t tx="amd.20160714230833.20"># setup static directory
@app.route('/static/<path:path>')
def send_file(path):
"""send file function"""
return app.send_static_file(static_dir + path)
</t>
<t tx="amd.20160714230833.3"># coding: utf-8
from flask import Flask, send_from_directory, request, redirect, \
render_template, session, make_response, url_for, flash
import random
import math
import os
# init.py 為自行建立的起始物件
import init
# 利用 nocache.py 建立 @nocache decorator, 讓頁面不會留下 cache
from nocache import nocache
# the followings are for cmsimfly
import re
import os
import math
import hashlib
# use quote_plus() to generate URL
import urllib.parse
# use cgi.escape() to resemble php htmlspecialchars()
# use cgi.escape() or html.escape to generate data for textarea tag, otherwise Editor can not deal with some Javascript code.
import cgi
import sys
# for new parse_content function
#from bs4 import BeautifulSoup
# 為了使用 bs4.element, 改為 import bs4
import bs4
# for ssavePage and savePage
import shutil
# get the current directory of the file
_curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
sys.path.append(_curdir)
# 由 init.py 中的 uwsgi = False 或 True 決定在 uwsgi 模式或近端模式執行
#ends for cmsimfly
# 假如隨後要利用 blueprint 架構時, 可以將程式放在子目錄中
# 然後利用 register 方式導入
# 導入 g1 目錄下的 user1.py
#import users.g1.user1
# 確定程式檔案所在目錄, 在 Windows 有最後的反斜線
_curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
# 表示程式在近端執行, 最後必須決定是由 init.py 或此地決定目錄設定
config_dir = _curdir + "/config/"
static_dir = _curdir + "/static"
download_dir = _curdir + "/downloads/"
image_dir = _curdir + "/images/"
# 利用 init.py 啟動, 建立所需的相關檔案
initobj = init.Init()
# 取 init.py 中 Init 類別中的 class uwsgi 變數 (static variable) 設定
uwsgi = init.Init.uwsgi
# 必須先將 download_dir 設為 static_folder, 然後才可以用於 download 方法中的 app.static_folder 的呼叫
app = Flask(__name__)
# 設置隨後要在 blueprint 應用程式中引用的 global 變數
app.config['config_dir'] = config_dir
app.config['static_dir'] = static_dir
app.config['download_dir'] = download_dir
# 使用 session 必須要設定 secret_key
# In order to use sessions you have to set a secret key
# set the secret key. keep this really secret:
app.secret_key = 'A0Zr9@8j/3yX R~XHH!jmN]LWX/,?R@T'
</t>
<t tx="amd.20160714230833.4"># 子目錄中註冊藍圖位置
#app.register_blueprint(users.g1.user1.g1app)
</t>
<t tx="amd.20160714231055.1"># seperate page need heading and edit variables, if edit=1, system will enter edit mode
# single page edit will use ssavePage to save content, it means seperate save page
@app.route('/get_page')
@app.route('/get_page/<heading>', defaults={'edit': 0})
@app.route('/get_page/<heading>/<int:edit>')
def get_page(heading, edit):
head, level, page = parse_content()
directory = render_menu(head, level, page)
if heading is None:
heading = head[0]
# 因為同一 heading 可能有多頁, 因此不可使用 head.index(heading) 搜尋 page_order
page_order_list, page_content_list = search_content(head, page, heading)
return_content = ""
pagedata = ""
outstring = ""
outstring_duplicate = ""
pagedata_duplicate = ""
outstring_list = []
for i in range(len(page_order_list)):
#page_order = head.index(heading)
page_order = page_order_list[i]
if page_order == 0:
last_page = ""
else:
last_page = head[page_order-1] + " << <a href='/get_page/" + \
head[page_order-1] + "'>Previous</a>"
if page_order == len(head) - 1:
# no next page
next_page = ""
else:
next_page = "<a href='/get_page/"+ head[page_order+1] + \
"'>Next</a> >> " + head[page_order+1]
if len(page_order_list) > 1:
return_content += last_page + " " + next_page + \
"<br /><h1>" + heading + "</h1>" + \
page_content_list[i] + "<br />"+ \
last_page + " " + next_page + "<br /><hr>"
pagedata_duplicate = "<h"+level[page_order] + ">" + heading + \
"</h"+level[page_order] + ">" + page_content_list[i]
outstring_list.append(last_page + " " + next_page + "<br />" + tinymce_editor(directory, cgi.escape(pagedata_duplicate), page_order))
else:
return_content += last_page + " " + next_page + "<br /><h1>" +\
heading + "</h1>" + page_content_list[i] + "<br />" + last_page + " " + next_page
pagedata += "<h"+level[page_order] + ">" + heading + "</h" + level[page_order] + ">" + page_content_list[i]
# 利用 cgi.escape() 將 specialchar 轉成只能顯示的格式
outstring += last_page + " " + next_page + "<br />" + tinymce_editor(directory, cgi.escape(pagedata), page_order)
# edit=0 for viewpage
if edit == 0:
return set_css() + "<div class='container'><nav>" + \
directory + "</nav><section>" + return_content + "</section></div></body></html>"
# enter edit mode
else:
# check if administrator
if not isAdmin():
redirect(url_for('login'))
else:
if len(page_order_list) > 1:
# 若碰到重複頁面頁印, 且要求編輯, 則導向 edit_page
#return redirect("/edit_page")
for i in range(len(page_order_list)):
outstring_duplicate += outstring_list[i] + "<br /><hr>"
return outstring_duplicate
else:
#pagedata = "<h"+level[page_order]+">"+heading+"</h"+level[page_order]+">"+search_content(head, page, heading)
#outstring = last_page+" "+next_page+"<br />"+ tinymce_editor(directory, cgi.escape(pagedata), page_order)
return outstring
</t>
<t tx="amd.20160714231359.1">def render_menu(head, level, page, sitemap=0):
'''允許使用者在 h1 標題後直接加上 h3 標題, 或者隨後納入 h4 之後作為標題標註'''
directory = ""
# 從 level 數列第一個元素作為開端
current_level = level[0]
# 若是 sitemap 則僅列出樹狀架構而沒有套用 css3menu 架構
if sitemap:
directory += "<ul>"
else:
directory += "<ul id='css3menu1' class='topmenu'>"
# 逐一配合 level 數列中的各標題階次, 一一建立對應的表單或 sitemap
for index in range(len(head)):
# 用 this_level 取出迴圈中逐一處理的頁面對應層級, 注意取出值為 str
this_level = level[index]
# 若處理中的層級比上一層級高超過一層, 則將處理層級升級 (處理 h1 後直接接 h3 情況)
if (int(this_level) - int(current_level)) > 1:
#this_level = str(int(this_level) - 1)
# 考慮若納入 h4 也作為標題標註, 相鄰層級可能大於一層, 因此直接用上一層級 + 1
this_level = str(int(current_level) + 1)
# 若處理的階次比目前已經處理的階次大, 表示位階較低
# 其實當 level[0] 完全不會報告此一區塊
# 從正在處理的標題階次與前一個元素比對, 若階次低, 則要加入另一區段的 unordered list 標頭
# 兩者皆為 str 會轉為整數後比較
if this_level > current_level:
directory += "<ul>"
directory += "<li><a href='/get_page/" + head[index] + "'>" + head[index] + "</a>"
# 假如正在處理的標題與前一個元素同位階, 則必須再判定是否為另一個 h1 的樹狀頭
elif this_level == current_level:
# 若正在處理的標題確實為樹狀頭, 則標上樹狀頭開始標註
if this_level == 1:
# 這裡還是需要判定是在建立 sitemap 模式或者選單模式
if sitemap:
directory += "<li><a href='/get_page/" + head[index] + "'>" + head[index]+"</a>"
else:
directory += "<li class='topmenu'><a href='/get_page/" + head[index] + "'>" + head[index] + "</a>"
# 假如不是樹狀頭, 則只列出對應的 list
else:
directory += "<li><a href='/get_page/" + head[index] + "'>" + head[index] + "</a>"
# 假如正處理的元素比上一個元素位階更高, 必須要先關掉前面的低位階區段
else:
directory += "</li>"*(int(current_level) - int(level[index]))
directory += "</ul>"*(int(current_level) - int(level[index]))
if this_level == 1:
if sitemap:
directory += "<li><a href='/get_page/" + head[index] + "'>" + head[index] + "</a>"
else:
directory += "<li class='topmenu'><a href='/get_page/" + head[index] + "'>" + head[index] + "</a>"
else:
directory += "<li><a href='/get_page/" + head[index] + "'>" + head[index] + "</a>"
current_level = this_level
directory += "</li></ul>"
return directory
</t>
<t tx="amd.20160714231440.1"># use head title to search page content
'''
# search_content(head, page, search)
# 從 head 與 page 數列中, 以 search 關鍵字進行查詢
# 原先傳回與 search 關鍵字頁面對應的頁面內容
# 現在則傳回多重的頁面次序與頁面內容數列
find = lambda searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
head = ["標題一","標題二","標題三","標題一","標題四","標題五"]
search_result = find(head,["標題一"])[0]
page_order = []
page_content = []
for i in range(len(search_result)):
# 印出次序
page_order.append(search_result[i])
# 標題為 head[search_result[i]]
# 頁面內容則為 page[search_result[i]]
page_content.append(page[search_result[i]])
# 從 page[次序] 印出頁面內容
# 準備傳回 page_order 與 page_content 等兩個數列
'''
def search_content(head, page, search):
"""search content"""
''' 舊內容
return page[head.index(search)]
'''
find = lambda searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
search_result = find(head, [search])[0]
page_order = []
page_content = []
for i in range(len(search_result)):
# 印出次序
page_order.append(search_result[i])
# 標題為 head[search_result[i]]
# 頁面內容則為 page[search_result[i]]
page_content.append(page[search_result[i]])
# 從 page[次序] 印出頁面內容
# 準備傳回 page_order 與 page_content 等兩個數列
return page_order, page_content
</t>
<t tx="amd.20160714231555.1">def editorhead():
return '''
<br />
<!--<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>-->
<script src="/static/tinymce4/tinymce/tinymce.min.js"></script>
<script src="/static/tinymce4/tinymce/plugins/sh4tinymce/plugin.min.js"></script>
<link rel = "stylesheet" href = "/static/tinymce4/tinymce/plugins/sh4tinymce/style/style.css">
<script>
tinymce.init({
selector: "textarea",
height: 500,
element_format : "html",
language : "en",
valid_elements : '*[*]',
extended_valid_elements: "script[language|type|src]",
plugins: [
'advlist autolink lists link image charmap print preview hr anchor pagebreak',
'searchreplace wordcount visualblocks visualchars code fullscreen',
'insertdatetime media nonbreaking save table contextmenu directionality',
'emoticons template paste textcolor colorpicker textpattern imagetools sh4tinymce'
],
toolbar1: 'insertfile save undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent',
toolbar2: 'link image | print preview media | forecolor backcolor emoticons | code sh4tinymce',
relative_urls: false,
toolbar_items_size: 'small',
file_picker_callback: function(callback, value, meta) {
cmsFilePicker(callback, value, meta);
},
templates: [
{ title: 'Test template 1', content: 'Test 1' },
{ title: 'Test template 2', content: 'Test 2' }
],
content_css: [
'//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
'//www.tinymce.com/css/codepen.min.css'
]
});
function cmsFilePicker(callback, value, meta) {
tinymce.activeEditor.windowManager.open({
title: 'Uploaded File Browser',
url: '/file_selector?type=' + meta.filetype,
width: 800,
height: 550,
}, {
oninsert: function (url, objVals) {
callback(url, objVals);
}
});
};
</script>
'''
</t>
<t tx="amd.20160714231606.1">def tinymce_editor(menu_input=None, editor_content=None, page_order=None):
sitecontent =file_get_contents(config_dir + "content.htm")
editor = set_admin_css() + editorhead() + '''</head>''' + editorfoot()
# edit all pages
if page_order is None:
outstring = editor + "<div class='container'><nav>" + \
menu_input + "</nav><section><form method='post' action='savePage'> \
<textarea class='simply-editor' name='page_content' cols='50' rows='15'>" + \
editor_content + "</textarea><input type='submit' value='save'> \
</form></section></body></html>"
else:
# add viewpage button wilie single page editing
head, level, page = parse_content()
outstring = editor + "<div class='container'><nav>" + \
menu_input+"</nav><section><form method='post' action='/ssavePage'> \
<textarea class='simply-editor' name='page_content' cols='50' rows='15'>" + \
editor_content + "</textarea><input type='hidden' name='page_order' value='" + \
str(page_order) + "'><input type='submit' value='save'>"
outstring += '''<input type=button onClick="location.href='/get_page/''' + \
head[page_order] + \
''''" value='viewpage'></form></section></body></html>'''
return outstring
</t>
<t tx="amd.20160714231619.1">def parse_config():
if not os.path.isfile(config_dir+"config"):
# create config file if there is no config file
# default password is admin
password="admin"
hashed_password = hashlib.sha512(password.encode('utf-8')).hexdigest()
with open(config_dir + "config", "w", encoding="utf-8") as f:
f.write("siteTitle:CMSimfly \npassword:"+hashed_password)
config = file_get_contents(config_dir + "config")
config_data = config.split("\n")
site_title = config_data[0].split(":")[1]
password = config_data[1].split(":")[1]
return site_title, password
</t>
<t tx="amd.20160714231628.1">def file_selector_script():
return '''
<script language="javascript" type="text/javascript">
$(function(){
$('.a').on('click', function(event){
setLink();
});
});
function setLink (url, objVals) {
top.tinymce.activeEditor.windowManager.getParams().oninsert(url, objVals);
top.tinymce.activeEditor.windowManager.close();
return false;
}
</script>
'''
</t>
<t tx="amd.20160714231640.1"># 與 file_selector 配合, 用於 Tinymce4 編輯器的檔案選擇
def file_lister(directory, type=None, page=1, item_per_page=10):
files = os.listdir(directory)
total_rows = len(files)
totalpage = math.ceil(total_rows/int(item_per_page))
starti = int(item_per_page) * (int(page) - 1) + 1
endi = starti + int(item_per_page) - 1
outstring = file_selector_script()
notlast = False
if total_rows > 0:
outstring += "<br />"
if (int(page) * int(item_per_page)) < total_rows:
notlast = True
if int(page) > 1:
outstring += "<a href='"
outstring += "file_selector?type=" + type + \
"&amp;page=1&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + str(session.get('download_keyword'))
outstring += "'><<</a> "
page_num = int(page) - 1
outstring += "<a href='"
outstring += "file_selector?type=" + type + \
"&amp;page=" + str(page_num) + \
"&amp;item_per_page=" +str(item_per_page) + \
"&amp;keyword=" + str(session.get('download_keyword'))
outstring += "'>Previous</a> "
span = 10
for index in range(int(page)-span, int(page)+span):
if index>= 0 and index< totalpage:
page_now = index + 1
if page_now == int(page):
outstring += "<font size='+1' color='red'>" + str(page) + " </font>"
else:
outstring += "<a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(page_now) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + \
str(session.get('download_keyword'))
outstring += "'>" + str(page_now)+"</a> "
if notlast == True:
nextpage = int(page) + 1
outstring += " <a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(nextpage) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + \
str(session.get('download_keyword'))
outstring += "'>Next</a>"
outstring += " <a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(totalpage) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + \
str(session.get('download_keyword'))
outstring += "'>>></a><br /><br />"
if (int(page) * int(item_per_page)) < total_rows:
notlast = True
if type == "file":
outstring += downloadselect_access_list(files, starti, endi) + "<br />"
else:
outstring += imageselect_access_list(files, starti, endi) + "<br />"
else:
outstring += "<br /><br />"
if type == "file":
outstring += downloadselect_access_list(files, starti, total_rows) + "<br />"
else:
outstring += imageselect_access_list(files, starti, total_rows) + "<br />"
if int(page) > 1:
outstring += "<a href='"
outstring += "file_selector?type=" + type + \
"&amp;page=1&amp;item_per_page=" + str(item_per_page) + \
"&amp;keyword=" + str(session.get('download_keyword'))
outstring += "'><<</a> "
page_num = int(page) - 1
outstring += "<a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(page_num) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + \
str(session.get('download_keyword'))
outstring += "'>Previous</a>"
span = 10
for index in range(int(page)-span, int(page)+span):
if index >=0 and index < totalpage:
page_now = index + 1
if page_now == int(page):
outstring += "<font size='+1' color='red'>"+str(page)+" </font>"
else:
outstring += "<a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(page_now) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + \
str(session.get('download_keyword'))
outstring += "'>" + str(page_now) + "</a> "
if notlast == True:
nextpage = int(page) + 1
outstring += " <a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(nextpage) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + \
str(session.get('download_keyword'))
outstring += "'>Next</a>"
outstring += " <a href='"
outstring += "file_selector?type=" + type + "&amp;page=" + \
str(totalpage) + "&amp;item_per_page=" + \
str(item_per_page) + "&amp;keyword=" + str(session.get('download_keyword'))
outstring += "'>>></a>"
else:
outstring += "no data!"
if type == "file":
return outstring+"<br /><br /><a href='fileuploadform'>file upload</a>"
else:
return outstring+"<br /><br /><a href='imageuploadform'>image upload</a>"
</t>
<t tx="amd.20160714231903.1">def downloadlist_access_list(files, starti, endi):
"""List files function for download_list."""
# different extension files, associated links were provided
# popup window to view images, video or STL files, other files can be downloaded directly
# files are all the data to list, from starti to endi
# add file size
outstring = ""
for index in range(int(starti)-1, int(endi)):
fileName, fileExtension = os.path.splitext(files[index])
fileExtension = fileExtension.lower()
fileSize = sizeof_fmt(os.path.getsize(download_dir+"/"+files[index]))
# images files
if fileExtension == ".png" or fileExtension == ".jpg" or fileExtension == ".gif":
outstring += '<input type="checkbox" name="filename" value="' + \
files[index] + '"><a href="javascript:;" onClick="window.open(\'/images/'+ \
files[index] + '\',\'images\', \'catalogmode\',\'scrollbars\')">' + \
files[index] + '</a> (' + str(fileSize) + ')<br />'
# stl files
elif fileExtension == ".stl":
outstring += '<input type="checkbox" name="filename" value="' + \
files[index] + '"><a href="javascript:;" onClick="window.open(\'/static/viewstl.html?src=/downloads/' + \
files[index] + '\',\'images\', \'catalogmode\',\'scrollbars\')">' + \
files[index] + '</a> (' + str(fileSize) + ')<br />'
# flv files
elif fileExtension == ".flv":
outstring += '<input type="checkbox" name="filename" value="' + \
files[index] + '"><a href="javascript:;" onClick="window.open(\'/flvplayer?filepath=/downloads/' + \
files[index] + '\',\'images\', \'catalogmode\',\'scrollbars\')">' + files[index] + '</a> (' + str(fileSize) + ')<br />'
# direct download files
else:
outstring += "<input type='checkbox' name='filename' value='" + files[index] + \
"'><a href='/downloads/" + files[index] + "'>" + files[index] + \
"</a> (" + str(fileSize) + ")<br />"
return outstring
</t>
<t tx="amd.20160714231915.1">def imagelist_access_list(files, starti, endi):
# different extension files, associated links were provided
# popup window to view images, video or STL files, other files can be downloaded directly
# files are all the data to list, from starti to endi
# add file size
outstring = ""
for index in range(int(starti)-1, int(endi)):
fileName, fileExtension = os.path.splitext(files[index])
fileExtension = fileExtension.lower()
fileSize = sizeof_fmt(os.path.getsize(image_dir + "/" + files[index]))
# images files
if fileExtension == ".png" or fileExtension == ".jpg" or fileExtension == ".gif":
outstring += '<input type="checkbox" name="filename" value="' + files[index] + \
'"><a href="javascript:;" onClick="window.open(\'/images/' + \
files[index] + '\',\'images\', \'catalogmode\',\'scrollbars\')">' + \
files[index] + '</a> (' + str(fileSize) + ')<br />'
return outstring
</t>
<t tx="amd.20160714231925.1">def sizeof_fmt(num):
"""size formate"""
for x in ['bytes','KB','MB','GB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'TB')
</t>
<t tx="amd.20160714231934.1"># set_admin_css for administrator
def set_admin_css():
"""set css for admin"""
outstring = '''<!doctype html>
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>''' + init.Init.site_title + '''</title> \
<link rel="stylesheet" type="text/css" href="/static/cmsimply.css">
''' + syntaxhighlight()
outstring += '''
<script src="/static/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("ul.topmenu> li:has(ul) > a").append('<div class="arrow-right"></div>');
$("ul.topmenu > li ul li:has(ul) > a").append('<div class="arrow-right"></div>');
});
</script>
'''
# SSL for uwsgi operation
if uwsgi:
outstring += '''
<script type="text/javascript">
if ((location.href.search(/http:/) != -1) && (location.href.search(/login/) != -1)) \
window.location= 'https://' + location.host + location.pathname + location.search;
</script>
'''
site_title, password = parse_config()
outstring += '''
</head><header><h1>''' + site_title + '''</h1> \
<confmenu>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/sitemap">SiteMap</a></li>
<li><a href="/edit_page">Edit All</a></li>
<li><a href="''' + str(request.url) + '''/1">Edit</a></li>
<li><a href="/edit_config">Config</a></li>
<li><a href="/search_form">Search</a></li>
<li><a href="/imageuploadform">Image Upload</a></li>
<li><a href="/image_list">Image List</a></li>
<li><a href="/fileuploadform">File Upload</a></li>
<li><a href="/download_list">File List</a></li>
<li><a href="/logout">Logout</a></li>
<li><a href="/generate_pages">generate_pages</a></li>
'''
outstring += '''
</ul>
</confmenu></header>
'''
return outstring
</t>
<t tx="amd.20160714231944.1">def set_footer():
"""footer for page"""
return "<footer> \
<a href='/edit_page'>Edit All</a>| \
<a href='" + str(request.url) + "/1'>Edit</a>| \
<a href='edit_config'>Config</a> \
<a href='login'>login</a>| \
<a href='logout'>logout</a> \
<br />Powered by <a href='http://cmsimple.cycu.org'>CMSimply</a> \
</footer> \
</body></html>"
</t>
<t tx="amd.20160714231953.1">def file_get_contents(filename):
# open file in utf-8 and return file content
with open(filename, encoding="utf-8") as file:
return file.read()
</t>
<t tx="amd.20160714232059.1">def syntaxhighlight():
return '''
<script type="text/javascript" src="/static/syntaxhighlighter/shCore.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushJScript.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushJava.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushPython.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushSql.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushXml.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushPhp.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushLua.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushCpp.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushCss.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/shBrushCSharp.js"></script>
<link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/css/shCoreDefault.css"/>
<script type="text/javascript">SyntaxHighlighter.all();</script>
<!-- for LaTeX equations 暫時不用
<script src="https://scrum-3.github.io/web/math/MathJax.js?config=TeX-MML-AM_CHTML" type="text/javascript"></script>
<script type="text/javascript">
init_mathjax = function() {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\\\(","\\\\)"] ],
displayMath: [ ['$$','$$'], ["\\\\[","\\\\]"] ]
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}}
}
});
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
}
init_mathjax();
</script>
-->
<!-- 暫時不用
<script src="/static/fengari-web.js"></script>
<script type="text/javascript" src="/static/Cango-13v08-min.js"></script>
<script type="text/javascript" src="/static/CangoAxes-4v01-min.js"></script>
<script type="text/javascript" src="/static/gearUtils-05.js"></script>
-->
<!-- for Brython 暫時不用
<script src="https://scrum-3.github.io/web/brython/brython.js"></script>
<script src="https://scrum-3.github.io/web/brython/brython_stdlib.js"></script>
-->
<style>
img {
border:2px solid blue;
}
</style>
'''
</t>
<t tx="amd.20160714232140.1"># 與 file_selector 搭配的取檔程式
def downloadselect_access_list(files, starti, endi):
"""Accompanied with file_selector."""
outstring = ""
for index in range(int(starti)-1, int(endi)):
fileName, fileExtension = os.path.splitext(files[index])
fileSize = os.path.getsize(download_dir + "/" + files[index])
outstring += '''<input type="checkbox" name="filename" value="''' + \
files[index] + '''"><a href="#" onclick='window.setLink("/downloads/''' + \
files[index] + '''",0); return false;'>''' + files[index] + \
'''</a> (''' + str(sizeof_fmt(fileSize)) + ''')<br />'''
return outstring
</t>
<t tx="amd.20160714232156.1">def loadlist_access_list(files, starti, endi, filedir):
# different extension files, associated links were provided
# popup window to view images, video or STL files, other files can be downloaded directly
# files are all the data to list, from starti to endi
# add file size
outstring = ""
for index in range(int(starti)-1, int(endi)):
fileName, fileExtension = os.path.splitext(files[index])
fileExtension = fileExtension.lower()
fileSize = sizeof_fmt(os.path.getsize(config_dir + filedir + "_programs/" + files[index]))
# images files
if fileExtension == ".png" or fileExtension == ".jpg" or fileExtension == ".gif":
outstring += '<input type="checkbox" name="filename" value="' + files[index] + \