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
// wiki_page.rs
//
// Copyright 2025 nee <nee-git@hidamari.blue>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::booru::WikiEntry;
use crate::data::*;
use crate::send;
use crate::sidebar_booru_select::make_booru_selection_list;
use adw::glib::property::PropertySet;
use adw::prelude::*;
use gtk::glib;
use gtk::glib::Properties;
use gtk::glib::clone;
use gtk::subclass::prelude::*;
use std::cell::{Cell, RefCell};
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate, Properties)]
#[properties(wrapper_type = super::WikiPage)]
#[template(resource = "/blue/hidamari/boorus/ui/wiki_page.ui")]
pub struct WikiPage {
#[template_child]
pub split: TemplateChild<adw::OverlaySplitView>,
#[template_child]
pub window: TemplateChild<gtk::ScrolledWindow>,
#[template_child]
pub search: TemplateChild<gtk::SearchEntry>,
#[template_child]
pub submit_search: TemplateChild<gtk::Button>,
#[template_child]
pub booru_list: TemplateChild<adw::Bin>,
#[template_child]
pub wiki_history: TemplateChild<gtk::ListBox>,
#[template_child]
pub headline: TemplateChild<gtk::Label>,
#[template_child]
pub links_headline: TemplateChild<gtk::Label>,
#[template_child]
pub links: TemplateChild<gtk::ListBox>,
#[template_child]
pub content: TemplateChild<gtk::Label>,
#[template_child]
pub other_names_headline: TemplateChild<gtk::Label>,
#[template_child]
pub other_names: TemplateChild<gtk::ListBox>,
#[template_child]
pub search_images: TemplateChild<gtk::Button>,
#[template_child]
pub content_box: TemplateChild<gtk::Box>,
#[property(get, set)]
pub narrow: Cell<bool>,
pub on_status_page: Cell<bool>,
}
#[glib::object_subclass]
impl ObjectSubclass for WikiPage {
const NAME: &'static str = "WikiPage";
type Type = super::WikiPage;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
// You must call `Widget`'s `init_template()` within `instance_init()`.
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for WikiPage {
fn constructed(&self) {
self.parent_constructed();
}
}
impl WidgetImpl for WikiPage {}
impl BoxImpl for WikiPage {}
}
glib::wrapper! {
pub struct WikiPage(ObjectSubclass<imp::WikiPage>)
@extends gtk::Widget, gtk::Box,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}
impl WikiPage {
pub fn init(&self, state: &State) {
self.imp().on_status_page.set(true);
self.imp()
.booru_list
.set_child(Some(&make_booru_selection_list(state, true)));
let status_page = adw::StatusPage::builder()
.icon_name("search-global-symbolic")
.title("Wiki")
.description("Read Tag descriptions")
.build();
self.imp().window.set_child(Some(&status_page));
self.imp().search_images.set_visible(false);
let search = self.imp().search.clone();
self.imp().submit_search.connect_clicked(clone!(
#[weak]
search,
move |_| {
search.emit_activate();
}
));
let sender = &state.sender;
search.connect_activate(clone!(
#[strong]
sender,
move |this| {
send!(sender, Action::OpenWikiPage(this.text().to_string()));
}
));
self.bind_property("narrow", &self.imp().split.get(), "collapsed")
.flags(glib::BindingFlags::SYNC_CREATE)
.build();
self.imp().content.connect_activate_link(clone!(
#[strong]
sender,
move |_, url| {
if let Some(article_title) = url.strip_prefix("wiki:") {
send!(sender, Action::OpenWikiPage(article_title.to_string()));
glib::Propagation::Stop
} else {
glib::Propagation::Proceed
}
}
));
}
pub fn set_content(&self, entry: &WikiEntry) {
if self.imp().on_status_page.get() {
self.imp().on_status_page.set(false);
self.imp()
.window
.set_child(Some(&self.imp().content_box.get()));
}
self.imp().headline.set_text(&entry.title);
self.imp()
.content
.set_markup(&Self::parse_content_markup(&entry.body));
self.imp().other_names.remove_all();
for name in &entry.other_names {
let label = gtk::Label::new(Some(&name));
self.imp().other_names.append(&label);
}
self.imp()
.other_names_headline
.set_visible(!entry.other_names.is_empty());
let history_item = >k::Button::new();
history_item.add_css_class("flat");
history_item.set_label(&entry.title);
history_item.set_action_name(Some("app.open-wiki-page"));
history_item.set_action_target_value(Some(&glib::Variant::from(&entry.title)));
self.imp().wiki_history.insert(history_item, 0);
self.imp().links.remove_all();
for link in &entry.links {
let button = gtk::LinkButton::new(&link);
self.imp().links.append(&button);
}
self.imp()
.links_headline
.set_visible(!entry.links.is_empty());
self.imp().search_images.set_visible(true);
self.imp()
.search_images
.set_action_name(Some("app.open-browse-search"));
self.imp()
.search_images
.set_action_target_value(Some(&glib::Variant::from(&entry.title)));
}
// replace
// "\r" ""
// "\nh1" hi\n" "<span size="x-large">hi</span>"
//
// ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’
//
// "[[text]]" "<a href=\"wiki:text\">text</a>"
// "[[label|text]]" "<a href=\"wiki:text\">label</a>"
// "*" "•"
fn parse_content_markup(content: &str) -> String {
let re = regex::Regex::new(r"\n\s?h1.([^\n]+)\n").unwrap();
let content = re.replace_all(
&content,
"\n<span weight=\"bold\" size=\"xx-large\">$1</span>\n",
);
let re = regex::Regex::new(r"\n\s?h2.([^\n]+)\n").unwrap();
let content = re.replace_all(
&content,
"\n<span weight=\"bold\" size=\"x-large\">$1</span>\n",
);
let re = regex::Regex::new(r"\n\s?h3.([^\n]+)\n").unwrap();
let content = re.replace_all(
&content,
"\n<span weight=\"bold\" size=\"large\">$1</span>\n",
);
let re = regex::Regex::new(r"\n\s?h4.([^\n]+)\n").unwrap();
let content = re.replace_all(
&content,
"\n<span weight=\"bold\" size=\"medium\">$1</span>\n",
);
let re = regex::Regex::new(r"\[\[([^\|\]]+)\|([^\]]+)\]\]").unwrap();
let content = re.replace_all(&content, "<a href=\"wiki:$1\">$2</a>");
let re = regex::Regex::new(r"\[\[([^\]]+)\]\]").unwrap();
let content = re.replace_all(&content, "<a href=\"wiki:$1\">$1</a>");
let re = regex::Regex::new(r"\*").unwrap();
let content = re.replace_all(&content, "•");
content.to_string()
}
pub fn update_booru_selection(&self, state: &State) {
self.imp()
.booru_list
.set_child(Some(&make_booru_selection_list(state, true)));
}
}