aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSathariel <sathariel@cock.li>2018-03-17 18:37:53 -0600
committerSathariel <sathariel@cock.li>2018-03-17 18:37:53 -0600
commitb9d1116a10e048a6c094b54ea25dedaf64e6caf3 (patch)
treeef000df587897f9079b474f453d42a164d4d7ede
downloadbarmark-b9d1116a10e048a6c094b54ea25dedaf64e6caf3.zip
barmark-b9d1116a10e048a6c094b54ea25dedaf64e6caf3.tar
Initial commitHEADmaster
-rw-r--r--LICENSE25
-rw-r--r--README.txt5
-rw-r--r--barmark-html122
-rw-r--r--test.bm50
4 files changed, 202 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..51254c8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,25 @@
+BSD 2-Clause License
+
+Copyright (c) 2018, Sathariel
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..40bd54a
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,5 @@
+Simple awk script to convert text to html. Read test.bm to learn the syntax.
+
+Run it using:
+
+awk -f barmark-html test.bm > out.html
diff --git a/barmark-html b/barmark-html
new file mode 100644
index 0000000..4194912
--- /dev/null
+++ b/barmark-html
@@ -0,0 +1,122 @@
+function findtag(t, reg) {
+ while(match($0, reg)) {
+ sub(reg, simpletag(t, stylize(substr($0, RSTART+2, RLENGTH-3))), $0);
+ changes = 1;
+ }
+}
+
+function stylize(str) {
+ gsub(/&/, "\\\\\\&", str);
+ if(match(str, /^\[[^\]]*\]/)) {
+ style = " class=\"" substr(str, RSTART+1, RLENGTH-2) "\"";
+ return substr(str, RLENGTH+2);
+ }
+ style = "";
+ return substr(str, 2);
+}
+
+function simpletag(t, str) {
+ return "<" t style ">" str "</" t ">";
+}
+
+function link(str) {
+ i = index(str, " ");
+ return "<a href=\"" substr(str, 1, i-1) "\"" style ">" substr(str, i+1) "</a>"
+}
+
+function img(str) {
+ i = index(str, " ")
+ return "<img src=\"" substr(str, 1, i-1) "\" alt=\"" substr(str, i+1) "\"" style " />"
+}
+
+function replacetags() {
+ changes = 0;
+ while(match($0, /\|a(\[[^\]]*\])? [^ ]+ [^\|]+\|/)) {
+ sub(/\|a(\[[^\]]*\])? [^ ]+ [^\|]+\|/,
+ link(stylize(substr($0, RSTART+2, RLENGTH-3))), $0);
+ changes = 1;
+ }
+ while(match($0, /\|img(\[[^\]]*\])? [^ ]+ [^\|]+\|/)) {
+ sub(/\|img(\[[^\]]*\])? [^ ]+ [^\|]+\|/,
+ img(stylize(substr($0, RSTART+4, RLENGTH-5))), $0);
+ changes = 1;
+ }
+ findtag("strong", "\\|b(\\[[^\\]]*\\])? [^\\|]+\\|");
+ findtag("em", "\\|i(\\[[^\\]]*\\])? [^\\|]+\\|");
+ findtag("span", "\\|s(\\[[^\\]]*\\])? [^\\|]+\\|");
+}
+
+BEGIN {
+ tag = "p";
+ block = "";
+ changes = 0;
+ style = "";
+}
+
+/^\|code$/ {
+ block = "code";
+ print "<pre>";
+ next;
+}
+
+/^code\$\|$/ {
+ block = "";
+ print "</pre>";
+ next;
+}
+
+block == "code" {
+ print;
+ next;
+}
+
+/^\|#.*\|/ {
+ next;
+}
+
+/^\|\^ ([^\$]*[^\|]*)+\$\|$/ {
+ sub(/^\|\^ /, "", $0);
+ sub(/\$\|$/, "", $0);
+ replacetags();
+ while(changes) { replacetags() }
+ print;
+ next;
+}
+
+/^\|h[1-6](\[[^\]]*\])? [^\|]+\|$/ {
+ print simpletag("h" substr($0, 3, 1),
+ stylize(substr($0, 4, length($0) - 4)));
+ next;
+}
+
+/^\|div(\[[^\]]*\])?$/ {
+ stylize(substr($0, 5));
+ print "<div" style ">" ;
+ next;
+}
+
+/^div\$\|$/ {
+ print "</div>" ;
+ next;
+}
+
+/^\|list(\[[^\]]*\])?$/ {
+ block = "list";
+ stylize(substr($0, 6));
+ print "<ul" style ">" ;
+ next;
+}
+
+/^list\$\|$/ {
+ block = "";
+ print "</ul>" ;
+ next;
+}
+
+NF {
+ if(block == "list") { tag = "li" }
+ replacetags();
+ while(changes) { replacetags() }
+ print "<" tag ">" $0 "</" tag ">";
+ tag = "p";
+}
diff --git a/test.bm b/test.bm
new file mode 100644
index 0000000..6ad524e
--- /dev/null
+++ b/test.bm
@@ -0,0 +1,50 @@
+|h1 Barmark Testing|
+
+|h2 Basic text formatting|
+
+Every line is transformed to a paragraph by default. Blank lines are ignored, so they can be used freely to organize the content.
+
+To add |b bold text| use |b.
+To add |i italics text| use |i.
+Span elements are added with |s.
+However span aren't useful by itself, |s[red] you can add [class] definitions| to any tag.
+
+|h3 Lists|
+
+You can create list surrounding elements with |list and list$|
+
+|list
+Element 1
+Element 2
+Element 3
+list$|
+
+At this moment just unordered lists are implemented.
+
+|h3 Links and Images|
+
+To create links use
+|code
+|a /main.html Main page|
+code$|
+
+Similarly you can add images
+|code
+|img /image.png Alt text|
+code$|
+
+|h3 Code|
+
+|code
+All the lines between |code and code$| are printed without substitute tags.
+code$|
+
+|h3 Divisors|
+
+As span, they could be useful with [class] definitions, they're created surrounding any block with |div and div$|, single lines will become paragraphs and tags are interpreted.
+
+|h3 Avoid the creation of pararaphs|
+
+Finally a line starting with |^ and finishing with $| can be used to render tags without the <p> tags. Other tags are interpreted but it could be used also for add raw html.
+
+|^ <hr /> $|