forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.rs
208 lines (191 loc) Β· 4.69 KB
/
templates.rs
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
use {super::*, boilerplate::Boilerplate};
pub(crate) use {
crate::subcommand::server::ServerConfig,
block::BlockHtml,
children::ChildrenHtml,
clock::ClockSvg,
collections::CollectionsHtml,
home::HomeHtml,
iframe::Iframe,
input::InputHtml,
inscription::InscriptionHtml,
inscriptions::InscriptionsHtml,
inscriptions_block::InscriptionsBlockHtml,
metadata::MetadataHtml,
output::OutputHtml,
parents::ParentsHtml,
preview::{
PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
},
range::RangeHtml,
rare::RareTxt,
rune_balances::RuneBalancesHtml,
sat::SatHtml,
};
pub use {
blocks::BlocksHtml, rune::RuneHtml, runes::RunesHtml, status::StatusHtml,
transaction::TransactionHtml,
};
pub mod block;
pub mod blocks;
mod children;
mod clock;
pub mod collections;
mod home;
mod iframe;
mod input;
pub mod inscription;
pub mod inscriptions;
mod inscriptions_block;
mod metadata;
pub mod output;
mod parents;
mod preview;
mod range;
mod rare;
pub mod rune;
pub mod rune_balances;
pub mod runes;
pub mod sat;
pub mod status;
pub mod transaction;
#[derive(Boilerplate)]
pub(crate) struct PageHtml<T: PageContent> {
content: T,
config: Arc<ServerConfig>,
}
impl<T> PageHtml<T>
where
T: PageContent,
{
pub(crate) fn new(content: T, config: Arc<ServerConfig>) -> Self {
Self { content, config }
}
fn og_image(&self) -> String {
if let Some(domain) = &self.config.domain {
format!("https://{domain}/static/favicon.png")
} else {
"https://ordinals.com/static/favicon.png".into()
}
}
fn superscript(&self) -> String {
if self.config.chain == Chain::Mainnet {
"alpha".into()
} else {
self.config.chain.to_string()
}
}
}
pub(crate) trait PageContent: Display + 'static {
fn title(&self) -> String;
fn page(self, server_config: Arc<ServerConfig>) -> PageHtml<Self>
where
Self: Sized,
{
PageHtml::new(self, server_config)
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Foo;
impl Display for Foo {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "<h1>Foo</h1>")
}
}
impl PageContent for Foo {
fn title(&self) -> String {
"Foo".to_string()
}
}
#[test]
fn page() {
assert_regex_match!(
Foo.page(Arc::new(ServerConfig {
chain: Chain::Mainnet,
csp_origin: Some("https://signet.ordinals.com".into()),
domain: Some("signet.ordinals.com".into()),
index_sats: true,
..default()
}),),
r"<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<meta name=format-detection content='telephone=no'>
<meta name=viewport content='width=device-width,initial-scale=1.0'>
<meta property=og:title content='Foo'>
<meta property=og:image content='https://signet.ordinals.com/static/favicon.png'>
<meta property=twitter:card content=summary>
<title>Foo</title>
<link rel=alternate href=/feed.xml type=application/rss\+xml title='Inscription Feed'>
<link rel=icon href=/static/favicon.png>
<link rel=icon href=/static/favicon.svg>
<link rel=stylesheet href=/static/index.css>
<link rel=stylesheet href=/static/modern-normalize.css>
<script src=/static/index.js defer></script>
</head>
<body>
<header>
<nav>
<a href=/ title=home>Ordinals<sup>alpha</sup></a>
.*
<a href=/clock title=clock>.*</a>
<a href=/rare.txt title=rare>.*</a>
.*
<form action=/search method=get>
<input type=text .*>
<input class=icon type=image .*>
</form>
</nav>
</header>
<main>
<h1>Foo</h1>
</main>
</body>
</html>
"
);
}
#[test]
fn page_mainnet() {
assert_regex_match!(
Foo.page(Arc::new(ServerConfig {
chain: Chain::Mainnet,
csp_origin: None,
domain: None,
index_sats: true,
..default()
})),
r".*<nav>\s*<a href=/ title=home>Ordinals<sup>alpha</sup></a>.*"
);
}
#[test]
fn page_no_sat_index() {
assert_regex_match!(
Foo.page(Arc::new(ServerConfig {
chain: Chain::Mainnet,
csp_origin: None,
domain: None,
index_sats: false,
..default()
})),
r".*<nav>\s*<a href=/ title=home>Ordinals<sup>alpha</sup></a>.*<a href=/clock title=clock>.*</a>\s*<form action=/search.*",
);
}
#[test]
fn page_signet() {
assert_regex_match!(
Foo.page(Arc::new(ServerConfig {
chain: Chain::Signet,
csp_origin: None,
domain: None,
index_sats: true,
..default()
})),
r".*<nav>\s*<a href=/ title=home>Ordinals<sup>signet</sup></a>.*"
);
}
}