]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Markdown: basic spacing support
authorTimePath <andrew.hardaker1995@gmail.com>
Tue, 9 Feb 2016 09:33:36 +0000 (20:33 +1100)
committerTimePath <andrew.hardaker1995@gmail.com>
Tue, 9 Feb 2016 09:33:36 +0000 (20:33 +1100)
qcsrc/lib/_all.inc
qcsrc/lib/markdown.qh [new file with mode: 0644]

index 307b4bd081c6f35cbbfe75eb87925bdd0f44ac2c..9aaf524493719d0171363d086cfd29db0944bf05 100644 (file)
@@ -49,6 +49,7 @@
 #include "linkedlist.qh"
 #include "log.qh"
 #include "map.qh"
+#include "markdown.qh"
 #include "math.qh"
 #include "misc.qh"
 #include "net.qh"
diff --git a/qcsrc/lib/markdown.qh b/qcsrc/lib/markdown.qh
new file mode 100644 (file)
index 0000000..63a4182
--- /dev/null
@@ -0,0 +1,65 @@
+#include "test.qh"
+
+/**
+ * handle string spacing as markdown:
+ *   - two spaces escape a linebreak (otherwise text wraps)
+ *   - two linebreaks become a paragraph (remain unchanged)
+ */
+string markdown(string s)
+{
+       string buf = "";
+       int lines = 0;
+       int spaces = 0;
+       FOREACH_CHAR(s, true, {
+               switch (it) {
+                       default:
+                               for (; spaces > 0; --spaces) {
+                                       buf = strcat(buf, " ");
+                               }
+                               buf = strcat(buf, chr2str(it));
+                               break;
+                       case ' ':
+                               spaces += 1;
+                               break;
+                       case '\n':
+                               lines += 1;
+                               if (lines > 1) {
+                                       lines = 0;
+                                       spaces = 0;
+                                       buf = strcat(buf, "\n\n");
+                                       break;
+                               }
+                               if (spaces < 2) {
+                                       spaces = 1;
+                               } else {
+                                       spaces = 0;
+                                       buf = strcat(buf, "\n");
+                               }
+                               break;
+               }
+       });
+       return buf;
+}
+
+TEST(Markdown, LineWrap)
+{
+       #define X(expect, in) MACRO_BEGIN \
+               string out = markdown(in); \
+               EXPECT_TRUE(expect == out); \
+               LOG_INFO(expect); \
+               LOG_INFO(out); \
+       MACRO_END
+
+       // identity
+       X("lorem ipsum", "lorem ipsum");
+       // trim trailing space
+       X("lorem ipsum", "lorem ipsum ");
+       // allow manual input wrapping
+       X("lorem ipsum", "lorem\nipsum");
+       // line break
+       X("lorem\nipsum", "lorem  \nipsum");
+       // paragraph
+       X("lorem\n\nipsum", "lorem\n\nipsum");
+       SUCCEED();
+       #undef X
+}