Skip to content

Commit 72fbc17

Browse files
prandlaveluca93
authored andcommitted
Render announcements, question replies, and messages as markdown
1 parent ba32818 commit 72fbc17

7 files changed

Lines changed: 31 additions & 5 deletions

File tree

cms/server/admin/templates/announcements.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ <h2 id="title_announcements" class="toggling_on">Announcements</h2>
2424
</div>
2525
<div class="notification_text">
2626
<label for="new_text">Text</label>
27-
<textarea name="text" id="new_text" style="width: 100%" ></textarea>
27+
<textarea name="text" id="new_text" style="width: 100%" ></textarea><br/>
28+
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
2829
</div>
2930
<input type="submit"
3031
value="Add announcement" />

cms/server/admin/templates/participation.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ <h2 id="title_questions" class="toggling_on">Questions</h2>
179179
<div class="alternative_answer">
180180
Alternative answer:<br/>
181181
<textarea name="reply_question_text"></textarea><br/>
182+
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
182183
</div>
183184
<input type="submit" value="Send"/>
184185
</form>
@@ -210,7 +211,8 @@ <h2 id="title_messages" class="toggling_on">Messages</h2>
210211
</div>
211212
<div class="notification_text">
212213
Text:
213-
<textarea name="message_text" style="width: 100%" ></textarea>
214+
<textarea name="message_text" style="width: 100%" ></textarea><br/>
215+
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
214216
</div>
215217
<input type="submit" value="Send"/>
216218
</div>

cms/server/admin/templates/questions.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ <h1>Questions</h1>
121121
<div class="alternative_answer">
122122
Alternative answer:<br/>
123123
<textarea name="reply_question_text"></textarea><br/>
124+
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
124125
</div>
125126
<input type="submit" value="Send"/>
126127
</form>

cms/server/contest/static/cws_style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ div.question_list div.answer div.body,
9292
div.message_list div.message div.body {
9393
padding-top: 5px;
9494
clear: both;
95+
}
96+
97+
/* admin-sent messages have markdown formatting and thus don't need
98+
* mucking with whitespace, but user questions are plaintext */
99+
div.question_list div.question div.body {
95100
white-space: pre-line;
96101
}
97102

cms/server/contest/templates/communication.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h4 class="subject empty">{% trans %}(no subject){% endtrans %}</h4>
2222
{% endif %}
2323
<span class="timestamp">{{ msg.timestamp|format_datetime_smart }}</span>
2424
{% if msg.text|length > 0 %}
25-
<div class="body">{{ msg.text }}</div>
25+
<div class="body">{{ msg.text | markdown }}</div>
2626
{% endif %}
2727
</div>
2828
{% endfor %}
@@ -91,7 +91,7 @@ <h4 class="subject empty">{% trans %}(no subject){% endtrans %}</h4>
9191
{% endif %}
9292
<span class="timestamp">{{ msg.reply_timestamp|format_datetime_smart }}</span>
9393
{% if msg.reply_text|length > 0 %}
94-
<div class="body">{{ msg.reply_text }}</div>
94+
<div class="body">{{ msg.reply_text | markdown }}</div>
9595
{% endif %}
9696
</div>
9797
{% else %}
@@ -118,7 +118,7 @@ <h4 class="subject empty">{% trans %}(no subject){% endtrans %}</h4>
118118
{% endif %}
119119
<span class="timestamp">{{ msg.timestamp|format_datetime_smart }}</span>
120120
{% if msg.text|length > 0 %}
121-
<div class="body">{{ msg.text }}</div>
121+
<div class="body">{{ msg.text | markdown }}</div>
122122
{% endif %}
123123
</div>
124124
{% endfor %}

cms/server/jinja2_toolbox.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from jinja2 import Environment, StrictUndefined, contextfilter, \
2929
contextfunction, environmentfunction
3030
from jinja2.runtime import Context
31+
import markdown_it
32+
import markupsafe
3133

3234
from cms import TOKEN_MODE_DISABLED, TOKEN_MODE_FINITE, TOKEN_MODE_INFINITE, \
3335
TOKEN_MODE_MIXED, FEEDBACK_LEVEL_FULL, FEEDBACK_LEVEL_RESTRICTED, \
@@ -138,6 +140,19 @@ def today(ctx: Context, dt: datetime) -> bool:
138140
== now.replace(tzinfo=utc).astimezone(timezone).date()
139141

140142

143+
def markdown_filter(text: str) -> markupsafe.Markup:
144+
"""Renders text as markdown and returns a Markup object
145+
corresponding to it.
146+
147+
text: The markdown text to render.
148+
149+
return: rendered HTML wrapped in the Markup class.
150+
"""
151+
md = markdown_it.MarkdownIt('js-default')
152+
html = md.render(text)
153+
return markupsafe.Markup(html)
154+
155+
141156
def instrument_generic_toolbox(env: Environment):
142157
env.globals["iter"] = iter
143158
env.globals["next"] = next
@@ -163,6 +178,7 @@ def instrument_generic_toolbox(env: Environment):
163178
env.filters["any"] = any_
164179
env.filters["dictselect"] = dictselect
165180
env.filters["make_timestamp"] = make_timestamp
181+
env.filters["markdown"] = markdown_filter
166182

167183
env.tests["contains"] = lambda s, p: p in s
168184
env.tests["endswith"] = lambda s, p: s.endswith(p)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ chardet>=3.0,<5.3 # https://pypi.python.org/pypi/chardet
1515
babel==2.12.1 # http://babel.pocoo.org/en/latest/changelog.html
1616
pyxdg>=0.26,<0.29 # https://freedesktop.org/wiki/Software/pyxdg/
1717
Jinja2>=2.10,<2.11 # http://jinja.pocoo.org/docs/latest/changelog/
18+
markdown-it-py==3.0.0 # https://github.com/executablebooks/markdown-it-py/blob/master/CHANGELOG.md
1819

1920
# See https://github.com/pallets/markupsafe/issues/286 but breaking change in
2021
# MarkupSafe causes jinja to break

0 commit comments

Comments
 (0)