Skip to content

Commit

Permalink
finish the owl
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmarlow committed Mar 2, 2024
1 parent d868fa0 commit 48a544d
Show file tree
Hide file tree
Showing 6 changed files with 795 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web:
awk -v head=' <link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">' -f awkdown.awk README.md > demo.html
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@ A Markdown renderer written in Awk.
awk -f awkdown.awk README.md
```

---

## Demo

### Subsection one
Here's an [example link](https://www.mgmarlow.com/).

How about some _italicized text_? Maybe you're *feeling bold* instead.

# Header one

## Header two

### Header three

#### Header four

**Todos:**
Unordered list:

- Buy trail mix
- Eat trail mix

_numbered list_:
Numbered list:

1. Do one thing
2. Do another thing
Expand Down
75 changes: 65 additions & 10 deletions awkdown.awk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ BEGIN {
print "<head>"
print " <meta charset=\"utf-8\">"
print " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
if (head) print head
print "</head>"
print "<body>"
}
Expand All @@ -11,29 +12,83 @@ BEGIN {

END {
for (i = 1; i <= NR; i++)
render_line(data[i])
render(data[i])

# Flush any remaining open tags
if (inq)
render_line()
# Catch any trailing unclosed tags
if (inq) render()

print "</body>"
print "</html>"
}

function render_line(line) {
function render(line) {
if (line !~ /^> / && inquote) {
print "</blockquote>"
inquote = 0
}

if (line !~ /^- / && inul) {
print "</ul>"
inul = 0
}

if (line !~ /^[0-9]+. / && inol) {
print "</ol>"
inol = 0
}

if (match(line, /_(.*)_/, pats)) {
gsub(/_(.*)_/, sprintf("<em>%s</em>", pats[1]), line)
}

if (match(line, /\*(.*)\*/, pats)) {
gsub(/\*(.*)\*/, sprintf("<strong>%s</strong>", pats[1]), line)
}

if (match(line, /(.*)\[(.+)\]\((.+)\)(.*)/, pats)) {
gsub(/(.*)\[(.+)\]\((.+)\)(.*)/,
sprintf("%s<a href='%s'>%s</a>%s", pats[1], pats[3], pats[2], pats[4]), line)
}

if (line ~ /^# /) {
print "<h1>" substr(line, 3) "</h1>"
} else if (line ~ /^## /) {
print "<h2>" substr(line, 4) "</h2>"
} else if (line ~ /^### /) {
print "<h3>" substr(line, 5) "</h3>"
} else if (line ~ /^#### /) {
print "<h4>" substr(line, 6) "</h4>"
} else if (line ~ /^- /) {
if (!inul) {
print "<ul>"
inul = 1
}
print "<li>" substr(line, 3) "</li>"
} else if (line ~ /^[0-9]+. /) {
if (!inol) {
print "<ol>"
inol = 1
}
print "<li>" substr(line, 4) "</li>"
} else if (line ~ /^> /) {
if (!inq) {
if (!inquote) {
print "<blockquote>"
inq = 1
inquote = 1
}
print substr(line, 3)
} else if (line !~ /^> / && inq) {
print "</blockquote>"
inq = 0
} else if (line ~ /^```/) {
if (!inpre) {
print "<pre>"
inpre = 1
} else {
print "</pre>"
inpre = 0
}
} else if (inpre) {
print line
} else if (line ~ /^---$/) {
print "<hr />"
} else if (line !~ /^$/) {
print "<p>" line "</p>"
}
}
38 changes: 38 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!doctype html><html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<h1>Awkdown</h1>
<p>A Markdown renderer written in Awk.</p>
<h2>Usage</h2>
<pre>
awk -f awkdown.awk README.md
</pre>
<hr />
<h2>Demo</h2>
<p>Here's an <a href='https://www.mgmarlow.com/'>example link</a>.</p>
<p>How about some <em>italicized text</em>? Maybe you're <strong>feeling bold</strong> instead.</p>
<h1>Header one</h1>
<h2>Header two</h2>
<h3>Header three</h3>
<h4>Header four</h4>
<p>Unordered list:</p>
<ul>
<li>Buy trail mix</li>
<li>Eat trail mix</li>
</ul>
<p>Numbered list:</p>
<ol>
<li>Do one thing</li>
<li>Do another thing</li>
<li>Do the last thing</li>
</ol>
<blockquote>
Deep in the human unconscious is a pervasive need for a logical
universe that makes sense. But the real universe is always one step
beyond logic. - Frank Herbert
</body>
</html>

0 comments on commit 48a544d

Please sign in to comment.