commit 35733482a74f4875ecf828c17fdab71e9564ef4f Author: lovetocode999 Date: Sun Feb 28 15:00:12 2021 +0000 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..204b93d --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b679107 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# webify-gemini.sh + +A short script to convert entire gemini capsules to websites. + +Usage: + +``` +webify-gemini.sh +``` + +Example: + +``` +webify-gemini.sh /var/gemini/users/lovetocode999/ /home/lovetocode999/public_html/ gemini://gemini.ctrl-c.club/~lovetocode999/ http://ctrl-c.club/~lovetocode999/ +``` diff --git a/webify-gemini.sh b/webify-gemini.sh new file mode 100755 index 0000000..b086066 --- /dev/null +++ b/webify-gemini.sh @@ -0,0 +1,264 @@ +#!/bin/bash + +# Variables +if [ $# -eq 0 ]; then + echo -e "No arguments supplied. \n\nCorrect Usage:\n webify-gemini.sh \n\nExample:\n webify-gemini.sh /var/gemini/users/$USER/ /home/$USER/public_html/ gemini://gemini.ctrl-c.club/~$USER/ http://ctrl-c.club/~$USER/" + exit 1 +elif [ $# -eq 1 ]; then + if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then + echo -e "Usage:\n webify-gemini.sh \n\nExample:\n webify-gemini.sh /var/gemini/users/$USER/ /home/$USER/public_html/ gemini://gemini.ctrl-c.club/~$USER/ http://ctrl-c.club/~$USER/" + exit 0 + fi +elif ! [ $# -eq 4 ]; then + echo -e "Incorrect number of arguments.\n\nCorrect Usage:\n webify-gemini.sh \n\nExample:\n webify-gemini.sh /var/gemini/users/$USER/ /home/$USER/public_html/ gemini://gemini.ctrl-c.club/~$USER/ http://ctrl-c.club/~$USER/" + exit 1 +fi +geminiDirectory="$1" +htmlDirectory="$2" +geminiAddress="$3" +webAddress="$4" + +# Trim ending slash from varables +if [ "${geminiDirectory: -1}" == "/" ]; then + geminiDirectory=$(echo -n "$geminiDirectory" | rev | cut -c 2- | rev) +fi +if [ "${htmlDirectory: -1}" == "/" ]; then + htmlDirectory=$(echo -n "$htmlDirectory" | rev | cut -c 2- | rev) +fi +if [ "${geminiAddress: -1}" == "/" ]; then + geminiAddress=$(echo -n "$geminiAddress" | rev | cut -c 2- | rev) +fi +if [ "${webAddress: -1}" == "/" ]; then + webAddress=$(echo -n "$webAddress" | rev | cut -c 2- | rev) +fi + +# Find all .gmi and .gemini files in the user's gemini directory, then return +# the paths of these files relative to the user's gemini directory, omitting +# the file extension +function findFilesWithoutExtension() { + # All *.gmi files + find "$geminiDirectory/" -name '*.gmi' -printf "%P\n" | while read -r file + do + echo "$file" | rev | cut -f 2- -d '.' | rev + done + # All *.gemini files + find "$geminiDirectory/" -name '*.gemini' -printf "%P\n" | while read -r file + do + echo "$file" | rev | cut -f 2- -d '.' | rev + done +} + +# Find all subdirectories of the user's gemini directory +function findDirectories() { + find "$geminiDirectory/" -type d -printf "%P\n" +} + +# Make sure all the subdirectories exist +for directory in $(findDirectories) +do + mkdir -p "$htmlDirectory/$directory" +done + +# Copy all non-gemtext files into the user's public_html folder +find "$geminiDirectory/" -type f -printf "%P\n" | while read -r file +do + if [ "${file: -4}" != ".gmi" ] && [ "${file: -7}" != ".gemini" ]; then + cp "$geminiDirectory/$file" "$htmlDirectory/$file" + fi +done + +# Make sure $htmlDirectory/style.css exists +if ! test -f "$htmlDirectory/style.css"; then + echo "/* This file controls the styling of your website. For an example style.css +file, see http://ctrl-c.club/~lovetocode999/style.css */ + +/* Normal text */ +p {} + +/* Links */ +a {} + +/* Headers */ +h1 {} + +/* Sub-headers */ +h2 {} + +/* Sub-sub-headers */ +h3 {} + +/* Bulleted lists */ +ul {} + +/* List items */ +li {} + +/* Blockquotes */ +blockquote {} + +/* Preformatted text */ +pre {} + +/* Applies to all separate elements */ +* {} + +/* Applies to the entire page as one element */ +body {}" > "$htmlDirectory/style.css" +fi + +# Convert all the gemtext files into html files, and copy them into the user's +# public_html directory +for file in $(findFilesWithoutExtension) +do + # Check whether file is a .gmi or .gemini file + if test -f "$geminiDirectory/$file.gmi"; then + filename="$geminiDirectory/$file.gmi" + else + filename="$geminiDirectory/$file.gemini" + fi + # Set the title to the content of the first header + title=$(awk 'BEGIN{FPAT="([^ ]+)"}{if ($1=="#"){$1="";$2="\b"$2;print $0;exit}}' "$filename") + + # Run the file through awk, passing the gemini address, web address and + # title to awk as varables + awk -v title="$title" -v geminiAddress="$geminiAddress" -v webAddress="$webAddress" ' + BEGIN { + # Split the file by spaces + FPAT = "([^ ]+)" + # Print the start of the html file, up to the tag + print "\n\n\n \n "title"\n \n\n" + # These varables will be used later on + urlcount=0 # Number of urls + headnum=0 # Number of headers + subheadnum=0 # Number of sub-headers + subsubheadnum=0 # Number of sub-sub-headers + pre=0 # Whether or not the current text is preformatted + bullist=0 # Whether or not a bulleted list was just formed + } + { + # If the line is ```, toggle preformatted text + if ($1 == "```") { + # The next four lines check if the previous line was part of a + # bulleted list, and, if it was, print a closing tag. These + # four lines are present at the start of every if expression, + # except for the one that prints an actual list + if (bullist) { + bullist = !bullist + print " " + } + # Toggle pre + pre = !pre + # If pre, print a
 tag, else, print a closing 
tag + if (pre) { + print "
"
+            }
+            else {
+                print "    
" + } + } + # If the text is preformatted, just print it without checking for any + # formatting + else if (pre) { + print $0 + } + # Bulleted list + else if ($1 == "*") { + # We do not need to print the asterisk, so set the variable holding + # it to an empty string + $1 = "" + # If bullist, print just an
  • tag containing the text, else + # print a starting
      as well as an
    • tag containing the text + if (bullist) { + print "
    • "$0"
    • " + } + else { + bullist = !bullist + print "
        \n
      • "$0"
      • " + } + } + # Blockquotes + else if ($1 == ">") { + if (bullist) { + bullist = !bullist + print "
      " + } + $1 = "" + print "
      "$0"
      " + } + # Headers + else if ($1 == "#") { + headnum++ + if (bullist) { + bullist = !bullist + print "
    " + } + $1 = "" + print "

    "$0"

    " + } + # Sub-headers + else if ($1 == "##") { + subheadnum++ + if (bullist) { + bullist = !bullist + print " " + } + $1 = "" + print "

    "$0"

    " + } + # Sub-sub-headers + else if ($1 == "###") { + subsubheadnum++ + if (bullist) { + bullist = !bullist + print " " + } + $1 = "" + print "

    "$0"

    " + } + # Links + else if ($1 == "=>") { + if (bullist) { + bullist = !bullist + print " " + } + # Increment the number of urls + urlcount++ + # Substitute all occurrences of .gmi and .gemini with .html, + # substitute all occurrences of $geminiAddress with $webAddress, + sub(geminiAddress, webAddress) + if ($2 ~ webAddress) { + sub(/\.gmi/, ".html") + sub(/\.gemini/, ".html") + } + # Set the url to the second argument + url = $2 + # Set the first two arguments to nothing, so only the link text is + # printed + $1 = "" + $2 = "" + # Print a href tag with url as the url text and $0 as the link text, + # then print a
    tag so that multiple consecutive urls do not + # end up on the same line + print " "$0"
    " + } + # If none of these previous formatting statements run, just print the + # text in a

    tag + else { + if (bullist) { + bullist = !bullist + print " " + } + print "

    "$0"

    " + } + } + END { + if (bullist) { + bullist = !bullist + print " " + } + # Finally, print a closing and + print "\n" + } + ' "$filename" > "$htmlDirectory/$file.html" + echo "Converted ${filename} to ${htmlDirectory}/${file}.html" +done