2222from PIL import ImageFont , ImageDraw , Image , ImageTk
2323from pathlib import Path
2424
25+ #-------------------------------------------
26+ # Constants
27+ #-------------------------------------------
28+
29+ DEFAULT_CSS = """
30+ table, th, td {
31+ border: 1px solid black;
32+ border-collapse: collapse;
33+ }
34+
35+ blockquote {
36+ background-color: #e0e0e0;
37+ }
38+
39+ pre code {
40+ background-color: #e0e0e0;
41+ font-family: monospace;
42+ display: block;
43+ }
44+
45+ p code {
46+ font-family: monospace;
47+ color: #c03030;
48+ }
49+ """
50+
2551#-------------------------------------------
2652# Persistence
2753#-------------------------------------------
@@ -32,7 +58,19 @@ def __init__(self):
3258 self .__mkdir (self .__basepath )
3359 self .__notespath = os .path .join (self .__basepath , "notes" )
3460 self .__mkdir (self .__notespath )
61+ self .__css = self .__load_css ()
3562
63+ def __load_css (self ):
64+ css_file = os .path .join (self .__basepath , "style.css" )
65+ if os .path .isfile (css_file ):
66+ with open (css_file , "rb" ) as f :
67+ css = f .read ().decode ("utf-8" )
68+ else :
69+ css = DEFAULT_CSS
70+ with open (css_file , "wb" ) as f :
71+ f .write (css .encode ("utf-8" ))
72+ return css
73+
3674 def __mkdir (self , path ):
3775 if not os .path .isdir (path ):
3876 os .mkdir (path )
@@ -84,8 +122,9 @@ def screenshot(self, name):
84122 status = os .system ('gnome-screenshot -a -f %s' % full_filename )
85123 exit_code = os .waitstatus_to_exitcode (status )
86124 return filename if 0 == exit_code else None
87-
88125
126+ def css (self ):
127+ return self .__css
89128
90129#-------------------------------------------
91130# Model
@@ -143,6 +182,9 @@ def screenshot(self):
143182
144183 def base_path (self ):
145184 return self .__persistence .note_path (self .__name )
185+
186+ def css (self ):
187+ return self .__persistence .css ()
146188
147189
148190class NoteCollection :
@@ -342,7 +384,7 @@ def update_view(self):
342384 contents = self .text .get (1.0 , tk .END )
343385 html = markdown .markdown (contents , extensions = ['tables' ])
344386 self .frame .load_html (html , base_url = "file://%s/" % self .note .base_path ())
345- self .frame .add_css (CSS )
387+ self .frame .add_css (self . nore . css () )
346388
347389 def update (self ):
348390 self .save ()
@@ -352,7 +394,7 @@ def update(self):
352394 contents = self .note .contents ()
353395 html = markdown .markdown (contents , extensions = ['tables' ])
354396 self .frame .load_html (html , base_url = "file://%s/" % self .note .base_path ())
355- self .frame .add_css (CSS )
397+ self .frame .add_css (self . note . css () )
356398 self .text .delete (1.0 , tk .END )
357399 self .text .insert (tk .END , contents )
358400 self .namevar .set (self .note .name ())
@@ -490,28 +532,6 @@ def run(self):
490532 "AQYBBwZwbHVzLTIGcGFwZXJzBGxvb2sGY2FtZXJhDXNwaW5uZXItYWx0LTMD"
491533 "YmluAAAAAf//AAIAAAABAAAAAN4GKm4AAAAA38SxUQAAAADfxLFR" )
492534
493- CSS = """
494- table, th, td {
495- border: 1px solid black;
496- border-collapse: collapse;
497- }
498-
499- blockquote {
500- background-color: #e0e0e0;
501- }
502-
503- pre code {
504- background-color: #e0e0e0;
505- font-family: monospace;
506- display: block;
507- }
508-
509- p code {
510- font-family: monospace;
511- color: #c03030;
512- }
513- """
514-
515535if __name__ == "__main__" :
516536 app = App ()
517537 app .run ()
0 commit comments