upgrading + draft v2 + markdown parser

This commit is contained in:
Simon Petit 2024-10-24 14:16:19 +02:00
parent 29d0344081
commit d024465573
7 changed files with 411 additions and 0 deletions

66
bob2 Executable file
View File

@ -0,0 +1,66 @@
#!/usr/bin/env bash
usage()
{
echo
echo "This script is made for those who want to blog and are also addicted to the command line"
echo
echo "Run the initiation for a start. After that, place all your future blog
posts, written in markdown (.md or .markdown), in the draft folder."
echo
echo "Once you publish your blog, all the drafts in the said folder will be
converted to html, added to the posts folder and append to the index.html"
echo
echo "To remove a post, just remove it from the draft folder and republish
your blog"
echo
echo "bob commands :"
echo " help display this help"
echo " init initiate the blog"
echo " publish publish the blog"
}
init()
{
echo Name of the author of the blog :
read author
echo "author:$author" > .blog.conf
echo Name of the blog :
read blog
echo "blog:$blog" >> .blog.conf
echo "Language of the blog : (en)"
read lang
if [ -z $lang ]; then
lang=en
fi
echo "lang:$lang" >> .blog.conf
echo "Activate dark mode : (y/N)"
read dark
if [ -z $dark ]; then
dark=n
fi
echo "dark:$dark" >> .blog.conf
mkdir drafts
mkdir bases
mkdir posts
mkdir css
_init_css
_index "$blog" "$lang" "$dark"
}
if [[ $# -eq 0 ]]; then
usage
elif [[ "$1" == "help" ]]; then
usage
elif [[ "$1" == "usage" ]]; then
usage
elif [[ "$1" == "init" ]]; then
init
elif [[ "$1" == "publish" ]]; then
publish
elif [[ "$1" == "help" ]]; then
usage
fi

150
lib/markdown.awk Executable file
View File

@ -0,0 +1,150 @@
#!/usr/bin/awk
BEGIN {
env = "none"
stack_pointer = 0
push(env)
}
# Function to push a value onto the stack
function push(value) {
stack_pointer++
stack[stack_pointer] = value
}
# Function to pop a value from the stack (LIFO)
function pop() {
if (stack_pointer > 0) {
value = stack[stack_pointer]
delete stack[stack_pointer]
stack_pointer--
return value
} else {
return "empty"
}
}
# Function to get last value in LIFO
function last() {
return stack[stack_pointer]
}
function replaceEmAndStrong(line, result, start, end) {
# Replace occurrences of **...** with <strong>...</strong>
while (match(line, /\*\*([^*]+)\*\*/)) {
start = RSTART
end = RSTART + RLENGTH - 1
# Build the result: before match, <strong>, content, </strong>, after match
line = substr(line, 1, start-1) "<strong>" substr(line, start+2, RLENGTH-4) "</strong>" substr(line, end+1)
}
# Replace occurrences of *...* with <em>...</em>
while (match(line, /\*([^*]+)\*/)) {
start = RSTART
end = RSTART + RLENGTH - 1
# Build the result: before match, <em>, content, </em>, after match
line = substr(line, 1, start-1) "<em>" substr(line, start+1, RLENGTH-2) "</em>" substr(line, end+1)
}
return line
}
function closeOne() {
env = pop()
print "</" env ">"
}
# Matching headers
/^#+ / {
match($0, /#+ /);
n = RLENGTH;
print "<h" n-1 ">" substr($0, n + 1) "</h" n-1 ">"
}
# Matching blockquotes
/^> / {
env = last()
if (env == "blockquote")
{
# In a blockquote block only print the text
print substr($0, 3);
} else {
# Otherwise, init the blockquote block
push("blockquote")
print "<blockquote>\n" substr($0, 3)
}
}
# Matching unordered lists
/^[-+*] / {
env = last()
if (env == "ul" ) {
# In a unordered list block, print a new item
print "<li>" substr($0, 3) "</li>"
} else {
# Otherwise, init the unordered list block
push("ul")
print "<ul>\n<li>" substr($0, 3) "</li>"
}
}
# Matching ordered lists
/^[0-9]+\./ {
env = last()
if (env == "ol") {
# In a ordered list block, print a new item
print "<li>" substr($0, 4) "</li>"
} else {
# Otherwise, init the ordered list block
push("ol")
print "<ol>\n<li>" substr($0, 4) "</li>"
}
}
# Matching code block
/^( |\t)/ {
env = last()
match($0, /( |\t)/);
n = RLENGTH;
if (env == "code") {
# In a code block, print a new item
print substr($0, n+1)
} else {
# Otherwise, init the code block
push("pre")
push("code")
print "<pre><code>" substr($0, n+1)
}
}
# Matching a simple paragraph
!/^(#|\*|-|\+|>|`|$|\t| )/ {
env = last()
if (env == "none") {
# If no block, print a paragraph
print "<p>" replaceEmAndStrong($0) "</p>"
} else if (env == "blockquote") {
print $0
}
}
$0 == "" {
env = last()
while (env != "none") {
env = pop()
print "</" env ">"
env = last()
}
}
END {
env = last()
while (env != "none") {
env = pop()
print "</" env ">"
env = last()
}
}

0
lib/post.awk Normal file
View File

16
lib/template/post.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta charset="utf-8">
<title>simpet</title>
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<link href="https://fonts.googleapis.com/css?family=Cutive+Mono|IBM+Plex+Mono&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="./css/indexstyle.css">
</head>
<body>
<h1 class='title'>simpet</h1>
<article>
{{article}}
</article>
</body>
</html>

149
test/test.sh Executable file
View File

@ -0,0 +1,149 @@
#!/usr/bin/env bash
# Text color variables
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color (reset)
# Ensure a script is provided
if [ -z "$1" ]; then
echo "Usage: $0 <markdown_parser_script>"
exit 1
fi
PARSER="$1"
# Check if the parser script exists
if [ ! -f "$PARSER" ]; then
echo "Error: $PARSER not found!"
exit 1
fi
# Determine if it's AWK or Shell (optional second argument)
PARSER_TYPE="bash" # Default to bash script
if [ -n "$2" ]; then
PARSER_TYPE="$2"
fi
# Define test cases as an array of markdown inputs and expected outputs
declare -a tests=(
"Header 1"
"# Header 1"
"<h1>Header 1</h1>"
"Header 2"
"## Header 2"
"<h2>Header 2</h2>"
"Header 3"
"### Header 3"
"<h3>Header 3</h3>"
"Header 4"
"#### Header 4"
"<h4>Header 4</h4>"
"Header 5"
"##### Header 5"
"<h5>Header 5</h5>"
"Header 6"
"###### Header 6"
"<h6>Header 6</h6>"
"Multiple headers 1"
$'# Header 1\n## Header 2'
"<h1>Header 1</h1><h2>Header 2</h2>"
# "Multiple headers 2"
# $'### Header 3\n\n## Header 2\n\n# Header 1'
# "<h3>Header 3</h3><h2>Header 2</h2><h1>Header 1</h1>"
"Unordered List"
$'- item\n* item'
"<ul><li>item</li><li>item</li></ul>"
"Ordered List"
$'1. item1\n1. item1\n3. item3'
"<ol><li>item1</li><li>item1</li><li>item3</li></ol>"
"Blockquote 1"
"> test of blockquote"
"<blockquote>test of blockquote</blockquote>"
"Blockquote 2"
$'> line1\n> line2'
"<blockquote>line1line2</blockquote>"
"Blockquote 2"
$'> line1\nline2'
"<blockquote>line1line2</blockquote>"
"Code Block 1"
$' code1'
"<pre><code>code1</code></pre>"
"Code Block 2"
$'\tcode1'
"<pre><code>code1</code></pre>"
"Paragraph 1"
"paragraph 1"
"<p>paragraph 1</p>"
"Paragraph 2"
"paragraph *emphasis* and **strong**"
"<p>paragraph <em>emphasis</em> and <strong>strong</strong></p>"
"Mix Code blocks and paragraphs 1"
$'First paragraph\n\n code block'
"<p>First paragraph</p><pre><code>code block</code></pre>"
"Mix Code blocks and paragraphs 2"
$'First paragraph\n\n code1\n code2\n\nSecond paragraph'
"<p>First paragraph</p><pre><code>code1code2</code></pre><p>Second paragraph</p>"
# You can add more test cases following the same format...
)
input="# test"
expected="<h1>test</h1>"
# Function to run a single test case
run_test() {
local input="$1"
local expected="$2"
local actual=""
# Get the actual output from the parser
if [ "$PARSER_TYPE" == "awk" ]; then
# Run AWK script with the input
actual=$(echo "$input" | awk -f "$PARSER" | tr -d '\n')
else
# Assume it's a shell script, run it
actual=$(echo "$input" | bash "$PARSER" | tr -d '\n')
fi
# Compare the actual output with the expected output
if [ "$actual" == "$expected" ]; then
echo -e "${GREEN}Test Passed!${NC}"
else
echo -e "${RED}Test Failed!${NC}"
echo "Input:"
echo "$input"
echo "Expected:"
echo "$expected"
echo "Got:"
echo "$actual"
fi
}
# Main loop to run all test cases
num_tests=$((${#tests[@]} / 3)) # Divide by 2 because each test has input/output pair
for ((i = 0; i < num_tests; i++)); do
input="${tests[i * 3 + 1]}"
expected="${tests[i * 3 + 2]}"
echo "Test $((i + 1)):" ${tests[i * 3]}
run_test "$input" "$expected"
done

12
test/test01.md Normal file
View File

@ -0,0 +1,12 @@
# Major title
## Minor title
### Subtitle
- ici
- on
- est
- là
ok

18
test/test02.md Normal file
View File

@ -0,0 +1,18 @@
# test
salut à tous
- item 1
- item 2
## test1
okkk
> blockquote
> again
> again and again
code
paragraph *em* and **strong**