diff options
author | Jefferson Julio <[email protected]> | 2021-05-30 22:53:32 -0300 |
---|---|---|
committer | Jefferson Julio <[email protected]> | 2021-05-30 22:53:32 -0300 |
commit | 381bff129b70471e86e99a2d6b6a7e090f13287e (patch) | |
tree | 35d383b4ec622efd8fbbd49a72c44a5561a04f92 /utils | |
parent | e9c35a9eff9e0881df7d6a6e8d17b70ac37ad0fe (diff) | |
download | blog.sh-381bff129b70471e86e99a2d6b6a7e090f13287e.tar.bz2 blog.sh-381bff129b70471e86e99a2d6b6a7e090f13287e.zip |
Better article parsing, support for article code parsing (add line numbers to code blocks)
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/parse-article-content.sh | 62 | ||||
-rwxr-xr-x | utils/parse-article.sh | 4 |
2 files changed, 64 insertions, 2 deletions
diff --git a/utils/parse-article-content.sh b/utils/parse-article-content.sh new file mode 100755 index 0000000..40593c5 --- /dev/null +++ b/utils/parse-article-content.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Skip file metadata, first 3 lines + +sanitize-html-entities () { + echo "$1" | + sed 's/</\</g' | + sed 's/>/\>/g' +} + +parse-article-content-file () { + local PRE_TAG= + local IGNORE_NEXT_PRE_TAG= + local INSIDE_PRE_TAG= + local CONTENT + local TLINE= + + while IFS= read -r line; do + TLINE="$line" + + if [ -z "$INSIDE_PRE_TAG" ]; then + echo "$line" | grep '<pre role="code"' > /dev/null + if [ $? -eq 0 ]; then + PRE_TAG=$line + INSIDE_PRE_TAG=1 + TLINE="$line <!-- code-start -->" + fi + else + echo "$line" | grep '<pre' > /dev/null + if [ "$?" -eq 0 ]; then + IGNORE_NEXT_PRE_TAG=1 + TLINE="<code>$(sanitize-html-entities "$line")</code>" + fi + + echo "$line" | grep '</pre>' > /dev/null + if [ $? -eq 0 ]; then + if [ -n "$IGNORE_NEXT_PRE_TAG" ]; then + IGNORE_NEXT_PRE_TAG="" + TLINE="<code>$(sanitize-html-entities "$line")</code>" + else + INSIDE_PRE_TAG="" + PRE_TAG="" + TLINE="$line<!-- code-end -->" + fi + else + TLINE="<code>$(sanitize-html-entities "$line")</code>" + fi + + fi + + CONTENT="${CONTENT} +$TLINE" + done <<< $(echo "$ARTICLE_FILE_CONTENT" | tail -n +3) + + echo "$CONTENT" +} + +if [ "$DO_NOT_PROCESS_HTML" = "true" ]; then + ARTICLE_CONTENT=$(echo "$ARTICLE_FILE_CONTENT" | tail -n +3) +else + ARTICLE_CONTENT=$(parse-article-content-file) +fi diff --git a/utils/parse-article.sh b/utils/parse-article.sh index c3e540a..b64c0ff 100755 --- a/utils/parse-article.sh +++ b/utils/parse-article.sh @@ -1,6 +1,7 @@ #!/bin/bash ARTICLE_TITLE=$(basename "$ARTICLE_FILE") +ARTICLE_FILE_SANITIZED="$(sanitize-filename "$ARTICLE_FILE")" # Pick all file content ARTICLE_FILE_CONTENT=$(cat "$ARTICLE_FILE") @@ -8,8 +9,7 @@ ARTICLE_FILE_CONTENT=$(cat "$ARTICLE_FILE") # The first 3 lines of the file are metadata information ARTICLE_METADATA=$(echo "$ARTICLE_FILE_CONTENT" | head -n 3) -# Skip file metadata, first 3 lines -ARTICLE_CONTENT=$(echo "$ARTICLE_FILE_CONTENT" | tail -n +3) +source $SCRIPT_DIR/utils/parse-article-content.sh # Tags are placed on the first line of the file, delimited by commas IFS=',' read -ra ARTICLE_TAGS <<< "$(echo "$ARTICLE_METADATA" | head -n 1)" |