]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
doc: helper tool to convert from wiki to sphinx
authorJason Ish <ish@unx.ca>
Wed, 2 Dec 2015 21:23:52 +0000 (15:23 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 28 Sep 2016 11:11:10 +0000 (13:11 +0200)
doc/sphinx/convert.py [new file with mode: 0755]

diff --git a/doc/sphinx/convert.py b/doc/sphinx/convert.py
new file mode 100755 (executable)
index 0000000..c5e3c19
--- /dev/null
@@ -0,0 +1,71 @@
+#! /usr/bin/env python
+
+import sys
+import re
+import urlparse
+import os.path
+import urllib2
+from StringIO import StringIO
+
+import requests
+
+def fetch_images(url, dest):
+
+    print("Parsing image URLs from %s." % (url))
+    urlparts = urlparse.urlparse(url)
+    r = requests.get(url)
+    for m in re.finditer(r"(/attachments/[^\s]+\.png)\"", r.text):
+        filename = os.path.basename(m.group(1))
+        image_url = "%s://%s%s" % (
+            urlparts.scheme, urlparts.netloc, m.group(1))
+
+        if not os.path.exists(dest):
+            os.makedirs(dest)
+
+        if os.path.exists("%s/%s" % (dest, filename)):
+            print("Image %s already exists." % (filename))
+            continue
+
+        print("Fetching image %s." % (image_url))
+
+        open(os.path.join(dest, filename), "w").write(
+            urllib2.urlopen(image_url).read())
+
+def main():
+
+    url = sys.argv[1]
+    output = sys.argv[2]
+
+    fetch_images(url, output)
+
+    print("Fetching %s." % (url))
+    r = requests.get("%s.json" % url)
+    text = r.json()["wiki_page"]["text"]
+    text = text.replace("\r", "")
+
+    with open("%s.rst" % output, "w") as fileobj:
+        for line in StringIO(text):
+
+            # Images.
+            line = re.sub(
+                r"!([^\s]+)!", r"\n.. image:: %s/\1" % output, line)
+
+            # h1.
+            if line.startswith("h1."):
+                line = re.sub("^h1\.\s+", "", line)
+                line += "=" * (len(line) - 1) + "\n"
+
+            # h2.
+            if line.startswith("h2."):
+                line = re.sub("^h2\.\s+", "", line)
+                line += "-" * (len(line) - 1) + "\n"
+
+            # h3.
+            if line.startswith("h3."):
+                line = re.sub("^h3\.\s+", "", line)
+                line += "~" * (len(line) - 1) + "\n"
+
+            fileobj.write(line.encode("utf-8"))
+
+if __name__ == "__main__":
+    sys.exit(main())