Skip to content

Commit c818a12

Browse files
clean up for release
1 parent 9ebe6b8 commit c818a12

File tree

6 files changed

+157
-73
lines changed

6 files changed

+157
-73
lines changed

Project.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name = "GtkMarkdownTextView"
2+
uuid = "26d59822-f2b6-4a0d-bae1-4d8fc12fd86b"
3+
version = "0.1.0"
4+
5+
[deps]
6+
Gtk = "4c0ca9eb-093a-5379-98c5-f87ac0bbbf44"
7+
8+
[compat]
9+
julia = "1.0"
10+
Gtk = "0.17.0, 1.0"
11+
12+
[extras]
13+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14+
15+
[targets]
16+
test = ["Test"]

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,52 @@
66

77
A Widget to display Markdown formatted text:
88

9+
![screenshot](assets/GtkMarkdownTextView.png)
10+
911
```julia
1012
w = GtkWindow("")
1113

12-
md = "# test\n ## test\n*test* test **test**\n - test\n\ttest"
14+
md = """
15+
# h1 heading
16+
## h2 heading
17+
## h3 heading
18+
*italic* normal **bold**
19+
20+
code
21+
22+
> quote
23+
24+
- item 1
25+
- items 2
26+
27+
1. list
28+
2. list2
29+
30+
"""
31+
1332
v = MarkdownTextView(md)
1433
push!(w,v)
1534
showall(w)
1635
```
36+
37+
The constructor can take a prelude text and color settings :
38+
39+
```julia
40+
MarkdownTextView(m::String, prelude::String, mc::MarkdownColors = MarkdownColors())
41+
```
42+
43+
The color settings are defined as :
44+
45+
```julia
46+
struct MarkdownColors
47+
font_size::Int
48+
color::String
49+
background::String
50+
highlight_color::String
51+
highlight_background::String
52+
end
53+
54+
# default values
55+
MarkdownColors() = MarkdownColors(13, "#000", "#fff", "#111", "#eee")
56+
57+
```

REQUIRE

Lines changed: 0 additions & 3 deletions
This file was deleted.

assets/GtkMarkdownTextView.png

29.4 KB
Loading

src/GtkMarkdownTextView.jl

Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
module GtkMarkdownTextView
22

3-
using Gtk, GtkExtensions
3+
using Gtk
44
import Gtk.GtkTextIter
55

66
using Markdown
77

88
export MarkdownTextView, MarkdownColors
99

1010
struct MarkdownColors
11+
font_size::Int
1112
color::String
1213
background::String
1314
highlight_color::String
1415
highlight_background::String
1516
end
1617

17-
MarkdownColors() = MarkdownColors("#000","#fff","#111","#eee")
18+
MarkdownColors() = MarkdownColors(13, "#000", "#fff", "#111", "#eee")
1819

1920
mutable struct MarkdownTextView <: GtkTextView
2021

@@ -25,133 +26,151 @@ module GtkMarkdownTextView
2526
function MarkdownTextView(m::Markdown.MD, prelude::String, mc::MarkdownColors = MarkdownColors())
2627

2728
buffer = GtkTextBuffer()
28-
set_gtk_property!(buffer,:text,prelude)
29+
buffer.text[String] = prelude
2930
view = GtkTextView(buffer)
3031

31-
GtkExtensions.style_css(view,"window, view, textview, buffer, text {
32-
background-color: $(mc.background);
33-
color: $(mc.color);
34-
font-family: Monaco, Consolas, Courier, monospace;
35-
margin:0px;
36-
}"
32+
style_css(view,
33+
"window, view, textview, buffer, text {
34+
background-color: $(mc.background);
35+
color: $(mc.color);
36+
font-family: Monaco, Consolas, Courier, monospace;
37+
margin:0px;
38+
}"
3739
)
3840

39-
#set_gtk_property!(view,:margin_left,1)
40-
set_gtk_property!(view,:monospace,true)
41-
set_gtk_property!(view,:wrap_mode,true)
42-
43-
Gtk.create_tag(buffer, "normal", font="13")
44-
Gtk.create_tag(buffer, "h1", font="bold 15")
45-
Gtk.create_tag(buffer, "h2", font="bold 14")
46-
Gtk.create_tag(buffer, "bold", font="bold")
47-
Gtk.create_tag(buffer, "italic", font="italic")
48-
Gtk.create_tag(buffer, "code", font="bold", foreground=mc.highlight_color, background=mc.highlight_background)
49-
50-
insert_MD!(buffer,m)
51-
# tag(buffer,"normal",1,length(buffer))
41+
#set_gtk_property!(view, :margin_left, 1)
42+
view.monospace[Bool] = true
43+
view.wrap_mode[Bool] = true
44+
45+
fs = mc.font_size
46+
47+
Gtk.create_tag(buffer, "normal", font = "$fs")
48+
Gtk.create_tag(buffer, "h1", font = "bold $(fs+3)")
49+
Gtk.create_tag(buffer, "h2", font = "bold $(fs+2)")
50+
Gtk.create_tag(buffer, "h3", font = "bold $(fs+1)")
51+
Gtk.create_tag(buffer, "h4", font = "bold $(fs)")
52+
Gtk.create_tag(buffer, "h5", font = "$(fs)")
53+
Gtk.create_tag(buffer, "h6", font = "$(fs-1)")
54+
Gtk.create_tag(buffer, "bold", font = "bold $(fs)")
55+
Gtk.create_tag(buffer, "italic", font = "italic $fs")
56+
Gtk.create_tag(buffer, "code", font = "bold $fs",
57+
foreground=mc.highlight_color, background=mc.highlight_background)
58+
59+
insert_MD!(buffer, m)
60+
# tag(buffer, "normal", 1, length(buffer))
5261

53-
n = new(view.handle,view,buffer)
62+
n = new(view.handle, view, buffer)
5463
Gtk.gobject_move_ref(n, view)
5564
end
5665

57-
MarkdownTextView(m::String) = MarkdownTextView(Markdown.parse(m),"")
58-
MarkdownTextView(m::String,prelude::String, mc::MarkdownColors) = MarkdownTextView(Markdown.parse(m),prelude,mc)
59-
MarkdownTextView(m::String, mc::MarkdownColors) = MarkdownTextView(Markdown.parse(m),"",mc)
66+
MarkdownTextView(m::String) = MarkdownTextView(Markdown.parse(m), "")
67+
MarkdownTextView(m::String, prelude::String, mc::MarkdownColors = MarkdownColors()) = MarkdownTextView(Markdown.parse(m), prelude, mc)
68+
MarkdownTextView(m::String, mc::MarkdownColors) = MarkdownTextView(Markdown.parse(m), "", mc)
6069

6170
end
6271

63-
function tag(buffer,what,i,j)
64-
Gtk.apply_tag(buffer,what,
65-
GtkTextIter(buffer,i) , GtkTextIter(buffer,j)
72+
function tag(buffer, what, i, j)
73+
Gtk.apply_tag(buffer, what,
74+
GtkTextIter(buffer, i), GtkTextIter(buffer, j)
6675
)
6776
end
6877

69-
function insert_MD!(buffer,m::Markdown.Header,i)
78+
function style_css(w::Gtk.GtkWidget, css::String)
79+
sc = Gtk.G_.style_context(w)
80+
push!(sc, GtkCssProvider(data=css), 600)
81+
end
82+
83+
function insert_MD!(buffer, m::Markdown.Header{N}, i) where N
7084
ip = i
7185

72-
insert!(buffer," ")
86+
insert!(buffer, " ")
7387
i += 4
7488
for el in m.text
75-
i = insert_MD!(buffer,el,i)
89+
i = insert_MD!(buffer, el, i)
90+
end
91+
tag(buffer, "h$(min(N,4))", ip, i)
92+
i
93+
end
94+
95+
function insert_MD!(buffer, m::Markdown.BlockQuote, i)
96+
insert!(buffer, "")
97+
i += 3
98+
for el in m.content
99+
i = insert_MD!(buffer, el, i)
76100
end
77-
tag(buffer, "h1", ip, i)
78101
i
79102
end
80103

81-
function insert_MD!(buffer,m::String,i)
82-
insert!(buffer,m)
104+
function insert_MD!(buffer, m::String, i)
105+
insert!(buffer, m)
83106
i += length(m)
84107
end
85108

86-
function insert_MD!(buffer,m::Markdown.LaTeX,i)
87-
i = insert_MD!(buffer,m.formula,i)
109+
function insert_MD!(buffer, m::Markdown.LaTeX, i)
110+
i = insert_MD!(buffer, m.formula, i)
88111
end
89112

90-
function insert_MD!(buffer,m::Markdown.Paragraph,i)
91-
# insert!(buffer,"\n\n")
113+
function insert_MD!(buffer, m::Markdown.Paragraph, i)
114+
# insert!(buffer, "\n\n")
92115
# i += 2
93116
for el in m.content
94-
i = insert_MD!(buffer,el,i)
117+
i = insert_MD!(buffer, el, i)
95118
end
96119
i
97120
end
98121

99-
function insert_MD!(buffer,m::Markdown.Code,i)
100-
insert!(buffer,m.code)
122+
function insert_MD!(buffer, m::Markdown.Code, i)
123+
insert!(buffer, m.code)
101124
tag(buffer, "code", i, i+sizeof(m.code))
102125
i += length(m.code)
103126
end
104127

105-
function insert_MD!(buffer,m::Markdown.List,i)
106-
for it in m.items
107-
insert!(buffer," - ")
108-
i += 6
128+
function insert_MD!(buffer, m::Markdown.List, i)
129+
130+
marker = k -> m.ordered == -1 ? "" : "$(k)."
131+
for (k, it) in enumerate(m.items)
132+
insert!(buffer, " $(marker(k)) ")
133+
i += 6 + (m.ordered == 1)
109134
for el in it
110-
i = insert_MD!(buffer,el,i)
135+
i = insert_MD!(buffer, el, i)
111136
end
112-
insert!(buffer,"\n")
137+
insert!(buffer, "\n")
113138
i += 1
114139
end
115140
i
116141
end
117142

118-
function insert_MD!(buffer,m::Markdown.Italic,i)
119-
ip = i
120-
for el in m.text
121-
i = insert_MD!(buffer,el,i)
122-
end
123-
tag(buffer, "italic", ip, i)
124-
i
125-
end
126-
127-
function insert_MD!(buffer,m::Markdown.Bold,i)
143+
tagname(m::Markdown.Italic) = "italic"
144+
tagname(m::Markdown.Bold) = "bold"
145+
146+
function insert_MD!(buffer, m::T, i) where T <: Union{Markdown.Italic, Markdown.Bold}
128147
ip = i
129148
for el in m.text
130-
i = insert_MD!(buffer,el,i)
149+
i = insert_MD!(buffer, el, i)
131150
end
132-
tag(buffer, "bold", ip, i)
151+
tag(buffer, tagname(m), ip, i)
133152
i
134153
end
135154

136-
function insert_MD!(buffer,m,i)
137-
if isdefined(m,:text)
155+
function insert_MD!(buffer, m, i)
156+
if isdefined(m, :text)
138157
for el in m.text
139-
i = insert_MD!(buffer,el,i)
158+
i = insert_MD!(buffer, el, i)
140159
end
141160
end
142-
if isdefined(m,:content)
161+
if isdefined(m, :content)
143162
for el in m.content
144-
i = insert_MD!(buffer,el,i)
163+
i = insert_MD!(buffer, el, i)
145164
end
146165
end
147166
i
148167
end
149168

150-
function insert_MD!(buffer,m::Markdown.MD)
169+
function insert_MD!(buffer, m::Markdown.MD)
151170
i = length(buffer)+1
152171
for el in m.content
153-
i = insert_MD!(buffer,el,i)
154-
insert!(buffer,"\n\n")
172+
i = insert_MD!(buffer, el, i)
173+
insert!(buffer, "\n\n")
155174
i += 2
156175
end
157176
end

test/runtests.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ using Gtk
55

66
w = GtkWindow("")
77

8-
md = "# test\n ## test\n*test* test **test**\n - test\n\ttest"
8+
md = """
9+
IOBuffer([data::AbstractVector{UInt8}]; keywords...) -> IOBuffer
10+
Create an in-memory I/O stream, which may optionally operate on a pre-existing array.
11+
It may take optional keyword arguments:
12+
- `read`, `write`, `append`: restricts operations to the buffer; see `open` for details.
13+
- `truncate`: truncates the buffer size to zero length.
14+
- `maxsize`: specifies a size beyond which the buffer may not be grown.
15+
- `sizehint`: suggests a capacity of the buffer (`data` must implement `sizehint!(data, size)`).
16+
When `data` is not given, the buffer will be both readable and writable by default.
17+
"""
18+
19+
920
v = MarkdownTextView(md)
1021
push!(w,v)
1122
showall(w)

0 commit comments

Comments
 (0)