Skip to content

Commit 9927cfa

Browse files
committed
Allow customising base URLs
1 parent 4c80327 commit 9927cfa

6 files changed

Lines changed: 31 additions & 25 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ Needless to say, you should have the development package of Python installed. No
3131
>>> import dsl
3232
>>> dsl.to_html(''' [m0][b]com·mu·ta·tor[/b] [p]7[/p] {{id=000008943}} [c rosybrown]\[[/c][c darkslategray][b]commutator[/b][/c] [c darkslategray][b]commutators[/b][/c][c rosybrown]\][/c] [p]BrE[/p] [c darkgray] [/c][c darkcyan]\[ˈkɒmjuteɪtə(r)\][/c] [s]z_commutator__gb_1.wav[/s] [p]NAmE[/p] [c darkgray] [/c][c darkcyan]\[ˈkɑːmjuteɪtər\][/c] [s]z_commutator__us_1.wav[/s] [c orange] noun[/c] [c darkgray] ([/c][c green]physics[/c][c darkgray])[/c]
3333
... [m1][c darkmagenta][b]1.[/b][/c] {{d}}a device that connects a motor to the electricity supply{{/d}}
34-
... [m1][c darkmagenta][b]2.[/b][/c] {{d}}a device for changing the direction in which electricity flows{{/d}}''', 'test')
35-
(' <div style="margin-left: 0px;"><b>com·mu·ta·tor</b> <i><font color="green">7</font></i> <span style="color: rosybrown;">[</span><span style="color: darkslategray;"><b>commutator</b></span> <span style="color: darkslategray;"><b>commutators</b></span><span style="color: rosybrown;">]</span> <i><font color="green">BrE</font></i> <span style="color: darkgray;"> </span><span style="color: darkcyan;">[ˈkɒmjuteɪtə(r)]</span> <audio controls autoplay src="/api/cache/test/z_commutator__gb_1.wav">z_commutator__gb_1.wav</audio> <i><font color="green">NAmE</font></i> <span style="color: darkgray;"> </span><span style="color: darkcyan;">[ˈkɑːmjuteɪtər]</span> <audio controls src="/api/cache/test/z_commutator__us_1.wav">z_commutator__us_1.wav</audio> <span style="color: orange;"> noun</span> <span style="color: darkgray;"> (</span><span style="color: green;">physics</span><span style="color: darkgray;">)</span> </div><div style="margin-left: 9px;"><span style="color: darkmagenta;"><b>1.</b></span> a device that connects a motor to the electricity supply </div><div style="margin-left: 9px;"><span style="color: darkmagenta;"><b>2.</b></span> a device for changing the direction in which electricity flows</div>', ['z_commutator__gb_1.wav', 'z_commutator__us_1.wav'])
34+
... [m1][c darkmagenta][b]2.[/b][/c] {{d}}a device for changing the direction in which electricity flows{{/d}}''', '/static', '/lookup')
35+
(' <div style="margin-left: 0px;"><b>com·mu·ta·tor</b> <i><font color="green">7</font></i> <span style="color: rosybrown;">[</span><span style="color: darkslategray;"><b>commutator</b></span> <span style="color: darkslategray;"><b>commutators</b></span><span style="color: rosybrown;">]</span> <i><font color="green">BrE</font></i> <span style="color: darkgray;"> </span><span style="color: darkcyan;">[ˈkɒmjuteɪtə(r)]</span> <audio controls autoplay src="/api/cache/test/z_commutator__gb_1.wav">z_commutator__gb_1.wav</audio> <i><font color="green">NAmE</font></i> <span style="color: darkgray;"> </span><span style="color: darkcyan;">[ˈkɑːmjuteɪtər]</span> <audio controls src="/api/cache/test/z_commutator__us_1.wavargs">z_commutator__us_1.wav</audio> <span style="color: orange;"> noun</span> <span style="color: darkgray;"> (</span><span style="color: green;">physics</span><span style="color: darkgray;">)</span> </div><div style="margin-left: 9px;"><span style="color: darkmagenta;"><b>1.</b></span> a device that connects a motor to the electricity supply </div><div style="margin-left: 9px;"><span style="color: darkmagenta;"><b>2.</b></span> a device for changing the direction in which electricity flows</div>', ['z_commutator__gb_1.wav', 'z_commutator__us_1.wav'])
3636
```
3737

38-
The module has only one function: `to_html`, which takes two arguments: the DSL string and the dictionary name, and returns a tuple of two elements: the HTML string and a list of media file names.
38+
The module has only one function: `to_html`, which takes three arguments: the DSL string and the base URLs for static files and lookup, and returns a tuple of two elements: the HTML string and a list of media file names.
39+
40+
# To do
41+
42+
- Allow custom styling by putting the DSL tags into classes

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "dsl2html"
7-
version = "0.1.5"
7+
version = "0.2.0"
88
authors = [
99
{ name="Yi Xing", email="blandilyte@gmail.com" },
10-
]
10+
]0
1111
description = "DSL to HTML conversion"
1212
readme = "README.md"
1313
requires-python = ">=3.0"
@@ -18,4 +18,4 @@ classifiers = [
1818
]
1919

2020
[project.urls]
21-
"Homepage" = "https://github.com/Crissium/python-dsl"
21+
"Homepage" = "https://github.com/Crissium/python-dsl"

src/build.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,34 +222,34 @@ void builder::write_media(const node &n)
222222

223223
if (is_image(filename))
224224
{
225-
html_stream << "<img src=\"" << url_cache_root << filename << "\" alt=\"" << filename << "\"/>";
225+
html_stream << "<img src=\"" << base_url_static_files << filename << "\" alt=\"" << filename << "\"/>";
226226
}
227227
else if (is_audio(filename))
228228
{
229229
if (audio_found)
230230
{
231-
html_stream << "<audio controls src=\"" << url_cache_root << filename << "\">" << filename << "</audio>";
231+
html_stream << "<audio controls src=\"" << base_url_static_files << filename << "\">" << filename << "</audio>";
232232
}
233233
else
234234
{
235-
html_stream << "<audio controls autoplay src=\"" << url_cache_root << filename << "\">" << filename << "</audio>";
235+
html_stream << "<audio controls autoplay src=\"" << base_url_static_files << filename << "\">" << filename << "</audio>";
236236
audio_found = true;
237237
}
238238
}
239239
else if (is_video(filename))
240240
{
241-
html_stream << "<video controls src=\"" << url_cache_root << filename << "\">" << filename << "</video>";
241+
html_stream << "<video controls src=\"" << base_url_static_files << filename << "\">" << filename << "</video>";
242242
}
243243
else
244244
{
245-
html_stream << "<a href=\"" << url_cache_root << filename << "\">" << filename << "</a>";
245+
html_stream << "<a href=\"" << base_url_static_files << filename << "\">" << filename << "</a>";
246246
}
247247
}
248248

249249
void builder::write_ref(const node &n)
250250
{
251251
std::string headword = get_node_link(n);
252-
html_stream << "<a href=\"" << url_lookup_root << headword << "\">" << headword << "</a>";
252+
html_stream << "<a href=\"" << base_url_lookup << headword << "\">" << headword << "</a>";
253253
}
254254

255255
void builder::write_url(const node &n)
@@ -351,9 +351,10 @@ void builder::node_to_html(const node &n)
351351
}
352352
}
353353

354-
builder::builder(const std::string &name_dict) : url_cache_root("/api/cache/" + name_dict + "/"),
355-
url_lookup_root("/api/lookup/" + name_dict + "/"),
356-
audio_found(false)
354+
builder::builder(const std::string &base_url_static_files, const std::string &base_url_lookup)
355+
: base_url_static_files(base_url_static_files)
356+
, base_url_lookup(base_url_lookup)
357+
, audio_found(false)
357358
{
358359
}
359360

src/dsl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class builder
9494

9595
static std::string get_node_link(const node &n);
9696

97-
const std::string url_cache_root;
98-
const std::string url_lookup_root;
97+
const std::string base_url_static_files;
98+
const std::string base_url_lookup;
9999

100100
bool audio_found;
101101

@@ -125,7 +125,7 @@ class builder
125125
public:
126126
std::vector<std::string> resources_name;
127127

128-
builder(const std::string &name_dict);
128+
builder(const std::string &base_url_static_files, const std::string &base_url_lookup);
129129

130130
std::string get_html(const node &root);
131131
};

src/dslmodule.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
#include <Python.h>
22
#include "dsl.h"
33

4-
std::pair<std::string, std::vector<std::string>> to_html(const std::string &dsl, const std::string &name_dict)
4+
std::pair<std::string, std::vector<std::string>> to_html(const std::string &dsl, const std::string &base_url_static_files, const std::string &base_url_lookup)
55
{
66
dom tree(dsl);
7-
builder b(name_dict);
7+
builder b(base_url_static_files, base_url_lookup);
88
std::string html = b.get_html(tree.root);
99
return std::make_pair(html, b.resources_name);
1010
}
1111

1212
static PyObject *to_html_wrapper(PyObject *self, PyObject *args)
1313
{
1414
const char *dsl;
15-
const char *name_dict;
15+
const char *base_url_static_files;
16+
const char *base_url_lookup;
1617

17-
if (!PyArg_ParseTuple(args, "ss", &dsl, &name_dict))
18+
if (!PyArg_ParseTuple(args, "sss", &dsl, &base_url_static_files, &base_url_lookup))
1819
{
1920
return NULL;
2021
}
2122

22-
builder b(name_dict);
23+
builder b(base_url_static_files, base_url_lookup);
2324
std::string html;
2425

2526
Py_BEGIN_ALLOW_THREADS
2627
dom tree(dsl);
27-
html = b.get_html(tree.root);
28+
html = b.get_html(tree.root);
2829
Py_END_ALLOW_THREADS
2930

3031
PyObject *html_str = PyUnicode_DecodeUTF8(html.c_str(), html.length(), "strict");

src/parse.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ dom::dom(const std::string &dsl_text)
537537
text_node->text.push_back(ch);
538538
}
539539
}
540-
catch (std::exception)
540+
catch (std::exception const &)
541541
{
542542
}
543543

0 commit comments

Comments
 (0)