1
1
module GtkMarkdownTextView
2
2
3
- # package code goes here
3
+ using Gtk
4
+ using GtkExtensions
5
+ import Gtk. GtkTextIter
4
6
5
- end # module
7
+ using Markdown
8
+
9
+ export MarkdownTextView, MarkdownColors
10
+
11
+ struct MarkdownColors
12
+ color:: String
13
+ background:: String
14
+ highlight_color:: String
15
+ highlight_background:: String
16
+ end
17
+
18
+ MarkdownColors () = MarkdownColors (" #000" ," #fff" ," #111" ," #eee" )
19
+
20
+ mutable struct MarkdownTextView <: GtkTextView
21
+
22
+ handle:: Ptr{Gtk.GObject}
23
+ view:: GtkTextView
24
+ buffer:: GtkTextBuffer
25
+
26
+ function MarkdownTextView (m:: Markdown.MD , prelude:: String , mc:: MarkdownColors = MarkdownColors ())
27
+
28
+ buffer = GtkTextBuffer ()
29
+ set_gtk_property! (buffer,:text ,prelude)
30
+ view = GtkTextView (buffer)
31
+
32
+ GtkExtensions. style_css (view," window, view, textview, buffer, text {
33
+ background-color: $(mc. background) ;
34
+ color: $(mc. color) ;
35
+ font-family: Monaco, Consolas, Courier, monospace;
36
+ margin:0px;
37
+ }"
38
+ )
39
+
40
+ # set_gtk_property!(view,:margin_left,1)
41
+ set_gtk_property! (view,:monospace ,true )
42
+ set_gtk_property! (view,:wrap_mode ,true )
43
+
44
+ Gtk. create_tag (buffer, " normal" , font= " 13" )
45
+ Gtk. create_tag (buffer, " h1" , font= " bold 15" )
46
+ Gtk. create_tag (buffer, " h2" , font= " bold 14" )
47
+ Gtk. create_tag (buffer, " bold" , font= " bold" )
48
+ Gtk. create_tag (buffer, " italic" , font= " italic" )
49
+ Gtk. create_tag (buffer, " code" , font= " bold" , foreground= mc. highlight_color, background= mc. highlight_background)
50
+
51
+ insert_MD! (buffer,m)
52
+ # tag(buffer,"normal",1,length(buffer))
53
+
54
+ n = new (view. handle,view,buffer)
55
+ Gtk. gobject_move_ref (n, view)
56
+ end
57
+
58
+ MarkdownTextView (m:: String ) = MarkdownTextView (Base. Markdown. parse (m)," " )
59
+ MarkdownTextView (m:: String ,prelude:: String , mc:: MarkdownColors ) = MarkdownTextView (Base. Markdown. parse (m),prelude,mc)
60
+ MarkdownTextView (m:: String , mc:: MarkdownColors ) = MarkdownTextView (Base. Markdown. parse (m)," " ,mc)
61
+
62
+ end
63
+
64
+ function tag (buffer,what,i,j)
65
+ Gtk. apply_tag (buffer,what,
66
+ GtkTextIter (buffer,i) , GtkTextIter (buffer,j)
67
+ )
68
+ end
69
+
70
+ function insert_MD! (buffer,m:: Markdown.Header ,i)
71
+ ip = i
72
+
73
+ insert! (buffer," " )
74
+ i += 4
75
+ for el in m. text
76
+ i = insert_MD! (buffer,el,i)
77
+ end
78
+ tag (buffer, " h1" , ip, i)
79
+ i
80
+ end
81
+
82
+ function insert_MD! (buffer,m:: String ,i)
83
+ insert! (buffer,m)
84
+ i += length (m)
85
+ end
86
+
87
+ function insert_MD! (buffer,m:: Markdown.LaTeX ,i)
88
+ i = insert_MD! (buffer,m. formula,i)
89
+ end
90
+
91
+
92
+ function insert_MD! (buffer,m:: Markdown.Paragraph ,i)
93
+ # insert!(buffer,"\n\n")
94
+ # i += 2
95
+ for el in m. content
96
+ i = insert_MD! (buffer,el,i)
97
+ end
98
+ i
99
+ end
100
+
101
+ function insert_MD! (buffer,m:: Markdown.Code ,i)
102
+ insert! (buffer,m. code)
103
+ tag (buffer, " code" , i, i+ sizeof (m. code))
104
+ i += length (m. code)
105
+ end
106
+
107
+ function insert_MD! (buffer,m:: Markdown.List ,i)
108
+ for it in m. items
109
+ insert! (buffer," - " )
110
+ i += 6
111
+ for el in it
112
+ i = insert_MD! (buffer,el,i)
113
+ end
114
+ insert! (buffer," \n " )
115
+ i += 1
116
+ end
117
+ i
118
+ end
119
+
120
+ function insert_MD! (buffer,m:: Markdown.Italic ,i)
121
+ ip = i
122
+ for el in m. text
123
+ i = insert_MD! (buffer,el,i)
124
+ end
125
+ tag (buffer, " italic" , ip, i)
126
+ i
127
+ end
128
+
129
+ function insert_MD! (buffer,m:: Markdown.Bold ,i)
130
+ ip = i
131
+ for el in m. text
132
+ i = insert_MD! (buffer,el,i)
133
+ end
134
+ tag (buffer, " bold" , ip, i)
135
+ i
136
+ end
137
+
138
+ function insert_MD! (buffer,m,i)
139
+ if isdefined (m,:text )
140
+ for el in m. text
141
+ i = insert_MD! (buffer,el,i)
142
+ end
143
+ end
144
+ if isdefined (m,:content )
145
+ for el in m. content
146
+ i = insert_MD! (buffer,el,i)
147
+ end
148
+ end
149
+ i
150
+ end
151
+
152
+ function insert_MD! (buffer,m:: Markdown.MD )
153
+ i = length (buffer)+ 1
154
+ for el in m. content
155
+ i = insert_MD! (buffer,el,i)
156
+ insert! (buffer," \n\n " )
157
+ i += 2
158
+ end
159
+ end
160
+
161
+
162
+ end
163
+
0 commit comments