Skip to content

Commit 4de99e1

Browse files
moved out of GtkIDE
1 parent f51792d commit 4de99e1

File tree

3 files changed

+177
-11
lines changed

3 files changed

+177
-11
lines changed

REQUIRE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
julia 0.6
1+
Gtk
2+
GtkExtensions
3+
julia 0.7

src/GtkMarkdownTextView.jl

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,163 @@
11
module GtkMarkdownTextView
22

3-
# package code goes here
3+
using Gtk
4+
using GtkExtensions
5+
import Gtk.GtkTextIter
46

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+

test/runtests.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
using GtkMarkdownTextView
2-
@static if VERSION < v"0.7.0-DEV.2005"
3-
using Base.Test
4-
else
5-
using Test
6-
end
1+
using GtkMarkdownTextView, Test
2+
using Gtk
73

8-
# write your own tests here
9-
@test 1 == 2
4+
@testset "MarkdownTextView" begin
5+
6+
w = GtkWindow("")
7+
8+
md = "# test\n ## test\n*test* test **test**\n - test\n\ttest"
9+
v = MarkdownTextView(md)
10+
push!(w,v)
11+
showall(w)
12+
sleep(10)
13+
destroy(w)
14+
15+
end

0 commit comments

Comments
 (0)