git » blitiri » commit 703adea

Support arbitrary regexp replacements in rst text

author Alberto Bertogli
2022-08-20 19:20:47 UTC
committer Alberto Bertogli
2022-08-20 19:30:49 UTC
parent 57bf293a8be233f90d36da2e15cd0d1feee7733c

Support arbitrary regexp replacements in rst text

Based on original patch by Leandro Lucarella <llucax@gmail.com>.

blitiri.cgi +29 -1

diff --git a/blitiri.cgi b/blitiri.cgi
index dc0409a..fcfd37e 100755
--- a/blitiri.cgi
+++ b/blitiri.cgi
@@ -54,6 +54,24 @@ captcha_method = "title"
 # How many articles to show in the index
 index_articles = 10
 
+# Before rendering the rst, perform these regexp-based replacements.
+rst_regexp_replaces = [
+    (r'.. youtube:: (.*)',
+    r'''.. raw:: html
+
+         <iframe width="560" height="315"
+                 src="https://www.youtube.com/embed/\1"
+                 frameborder="0" allowfullscreen></iframe>
+    '''),
+	(r'.. vimeo:: (\w*)',
+    r'''.. raw:: html
+
+         <iframe src="https://player.vimeo.com/video/\1"
+                 width="500" height="281" frameborder="0"
+                 webkitallowfullscreen mozallowfullscreen
+                 allowfullscreen></iframe>
+	'''),
+]
 
 #
 # End of configuration
@@ -71,6 +89,7 @@ import calendar
 import zlib
 import urllib
 import cgi
+import re
 from docutils.core import publish_parts
 from docutils.utils import SystemMessage
 
@@ -918,7 +937,16 @@ class Article (object):
 		self.loaded = True
 
 	def to_html(self):
-		return rst_to_html(self.raw_content)
+		dirname = os.path.dirname
+		post_url = '/'.join([dirname(full_url), 'posts',
+				dirname(self.path)])
+		post_dir = '/'.join([data_path, dirname(self.path)])
+
+		rst = self.raw_content.replace('##POST_URL##', post_url)
+		rst = rst.replace('##POST_DIR##', post_dir)
+		for pattern, replacement in rst_regexp_replaces:
+			rst = re.sub(pattern, replacement, rst)
+		return rst_to_html(rst)
 
 	def to_vars(self):
 		return {