slweb

Једноставни генератор статичких веб страна
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 2c4a694288974fe411c170a7243743a8215c4e6e
родитељ 5e10f7f40a8930f2230351dc7b3df21d368aa619
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Fri, 20 Nov 2020 18:17:50 +0100

Added backticks; moved slweb{,.1} to .in files; added language reference to manpage

Diffstat:
M.gitignore | 6++++--
MMakefile | 36++++++++++++++++++++++++------------
Rexamples/index.slw -> examples/basic/index.slw | 0
Aexamples/includes/footer.slw | 4++++
Aexamples/includes/header.slw | 4++++
Aexamples/includes/index.css | 18++++++++++++++++++
Aexamples/includes/index.slw | 18++++++++++++++++++
Aexamples/includes/section01.slw | 4++++
Aexamples/includes/section02.slw | 4++++
Aexamples/includes/section03.slw | 4++++
Dslweb | 188-------------------------------------------------------------------------------
Dslweb.1 | 125-------------------------------------------------------------------------------
Aslweb.1.in | 191+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aslweb.in | 189+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
измењених датотека: 14, додавања: 464(+), брисања: 327(-)

diff --git a/.gitignore b/.gitignore @@ -9,6 +9,8 @@ nohup.out *.png *.gz - -examples/index.html +slweb +slweb.1 +examples/basic/index.html +examples/includes/index.html diff --git a/Makefile b/Makefile @@ -1,40 +1,52 @@ -BINDIR=/usr/local/bin -MANDIR=/usr/local/share/man/man1 -BROWSER=surf -EXAMPLE_PAGES=$(addsuffix /index.slw,examples) -EXAMPLE_PAGES_HTML=$(EXAMPLE_PAGES:.slw=.html) +VERSION = v0.2-beta +BINDIR = /usr/local/bin +MANDIR = /usr/local/share/man/man1 +BROWSER = surf +EXAMPLE_PAGES = $(addsuffix /index.slw,examples/basic examples/includes) +#EXAMPLE_PAGES += $(addprefix examples/includes, +EXAMPLE_PAGES_HTML = $(EXAMPLE_PAGES:.slw=.html) %.html: %.slw - ./slweb $^ > $@ + ./slweb $^ > $@ %.gz: % gzip -kf $< %.ps: %.1 - groff -mandoc -t -T ps $< > $@ + groff -mandoc -t -T ps $< > $@ %.pdf: %.1 - groff -mandoc -t -T pdf $< > $@ + groff -mandoc -t -T pdf $< > $@ -all: examples doc +%: %.in + cat $< | sed -e "s/%VERSION%/${VERSION}/g" > $@ + +all: in examples doc + +in: slweb slweb.1 + +slweb: slweb.in + cat $< | sed -e "s/%VERSION%/${VERSION}/g" > $@ + chmod 0755 slweb examples: ${EXAMPLE_PAGES_HTML} view: examples - $(BROWSER) examples/index.html + $(BROWSER) examples/basic/index.html doc: slweb.pdf slweb.1.gz install: all mkdir -p $(BINDIR) $(MANDIR) cp slweb $(BINDIR) + chmod 0755 $(BINDIR)/slweb cp slweb.1.gz $(MANDIR) uninstall: rm $(BINDIR)/slweb $(MANDIR)/slweb.1.gz 2>/dev/null -clean: slweb.1.gz slweb.pdf $(EXAMPLE_PAGES_HTML) +clean: slweb.1.gz slweb.pdf $(EXAMPLE_PAGES_HTML) slweb slweb.1 rm $^ 2>/dev/null -.PHONY: examples view doc install uninstall clean +.PHONY: in examples view doc install uninstall clean diff --git a/examples/index.slw b/examples/basic/index.slw diff --git a/examples/includes/footer.slw b/examples/includes/footer.slw @@ -0,0 +1,4 @@ +{footer} +Page footer +{/footer} + diff --git a/examples/includes/header.slw b/examples/includes/header.slw @@ -0,0 +1,4 @@ +{header} +Page header +{/header} + diff --git a/examples/includes/index.css b/examples/includes/index.css @@ -0,0 +1,18 @@ +header +{ + background: #ffa; + padding: 1em; +} + +main +{ + background: #ccf; + padding: 1em; +} + +footer +{ + background: #faa; + padding: 1em; +} + diff --git a/examples/includes/index.slw b/examples/includes/index.slw @@ -0,0 +1,18 @@ +site-name: Includes +site-desc: Includes demo +--- + +{include "header"} + +{main} +# Document index + +{include "section01"} + +{include "section02"} + +{include "section03"} +{/main} + +{include "footer"} + diff --git a/examples/includes/section01.slw b/examples/includes/section01.slw @@ -0,0 +1,4 @@ +## First section + +This is a first section, added through an `include` directive. + diff --git a/examples/includes/section02.slw b/examples/includes/section02.slw @@ -0,0 +1,4 @@ +## Second section + +This is a second section. + diff --git a/examples/includes/section03.slw b/examples/includes/section03.slw @@ -0,0 +1,4 @@ +## Third section + +This is a third section. + diff --git a/slweb b/slweb @@ -1,188 +0,0 @@ -#!/bin/bash - -BODY_ONLY=0 -CSSFILE=index.css -INFILE=index.slw -OUTFILE=index.html -VERSION=v0.1-beta - -function throw() -{ - echo $1 1>&2 - exit 1 -} - -function version() -{ - cat <<EOT -Slweb $VERSION -EOT - return 1 -} - -function usage() -{ - cat <<EOT -usage: $(basename $0) [-b|--body-only] [-d <dir>|--basedir <dir>] [-h|--help] [-v|--version] <file> -EOT - return 1 -} - -function begin_html() -{ -cat <<EOT -<!DOCTYPE html> -<html lang="en"> -EOT -} - -function end_html() -{ -cat <<EOT -</html> -EOT -} - -function begin_head() -{ -cat <<EOT -<head> -EOT -} - -function head() -{ -cat <<EOT - <title>${VARS[site-name]}</title> - <meta charset="utf8" /> - <meta name="viewport" content="width=device-width, initial-scale=1" /> - <meta name="generator" content="slweb" /> -EOT -} - -function add_css() -{ - cat <<EOT - <link rel="stylesheet" href="$1" /> -EOT -} - -function end_head() -{ -cat <<EOT -</head> -EOT -} - -function begin_body() -{ -cat <<EOT -<body> -EOT -} - -function body() -{ - BODY=$(echo "$1" | \ - sed -e's/#### \([^\n]*\)$/<h4>\1<\/h4>/g' \ - -e's/### \([^\n]*\)$/<h3>\1<\/h3>/g' \ - -e's/## \([^\n]*\)$/<h2>\1<\/h2>/g' \ - -e's/# \([^\n]*\)$/<h1>\1<\/h1>/g' \ - -e 's/\*\*\([^*]\+\)\*\*/<strong>\1<\/strong>/g' \ - -e 's/\*\([^*]\+\)\*/<em>\1<\/em>/g' \ - -e 's/_\([^_]\+\)_/<em>\1<\/em>/g' \ - -e 's/{\.\(.*\)}/<div class="\1">/' \ - -e 's/{\/\.\(.*\)}/<\/div><!--\1-->/' \ - -e 's/{#\(.*\)}/<div id="\1">/' \ - -e 's/{\/#\(.*\)}/<\/div><!--\1-->/' \ - -e "s/{made-by}/$MADEBY/g" \ - -e 's/{\([a-z-]\+\)}/<\1>/g' \ - -e 's/{\/\([a-z-]\+\)}/<\/\1>/g' \ - -e "s/\\[\\([^]]\\+\\)\\](\\([^)]\\+\\))/<a href=\"$(realpath --relative-to=$BASEDIR $(dirname $INFILE) | sed -e's/\//\\\//g')\/\\2\">\\1<\\/a>/" | \ - sed -e '1h;2,$H;$!d;g;s/\n\n\([^<{\n]\+\)/\n\n<p>\1/g' | \ - sed -e '1h;2,$H;$!d;g;s/\([^>}\n]\)\n\n/\1<\/p>\n\n/g' | \ - sed -e '1h;2,$H;$!d;g;s/\([^>}\n]\)$/\1<\/p>\n\n/g') - - INCLUDE=$(echo "$BODY" | \ - sed -e'/{include/s/.*{include \"\([.\/a-z ]\+\)\"}.*/\1/;t;d' | \ - sed 1q) - - while [ ! -z "$INCLUDE" ]; do - INCLUDE_BODY=$($0 -b -d $(dirname $INFILE) "$(dirname $INFILE)/$INCLUDE.slw") - BODY=$(echo "$BODY" | \ - sed -e"s/{include \\\"$(echo $INCLUDE | sed -e's/\//\\\//g')\\\"}/$(echo $INCLUDE_BODY | sed -e's/\//\\\//g')/") - INCLUDE=$(echo "$BODY" | \ - sed -e'/{include/s/.*{include \"\([.\/a-z ]\+\)\"}.*/\1/;t;d' | \ - sed 1q) - done - echo "$BODY" -} - -function end_body() -{ -cat <<EOT -</body> -EOT -} - -BASEDIR=. -MADEBY=$(cat <<EOT | sed -e's/\//\\\//g' -e"s/\"/\\\\\"/g" | sed -z -e's/\n/\\n/g' -<div id="made-by"> -Generated by <a href="https://github.com/Strahinja/slweb">slweb</a> -© 2020 Strahinya Radich. -</div><!--made-by--> -EOT -) - -while [ $# -gt 0 ]; do - case "$1" in - -v|--version) version; exit - ;; - -h|--help) usage; exit - ;; - -b|--body-only) BODY_ONLY=1 - ;; - -d|--basedir) shift; BASEDIR=$1 - ;; - *) INFILE=$1 - ;; - esac - shift -done - -[ -f $INFILE ] || throw "$INFILE not present" - -declare -A VARS -VAR_KEYS=$(grep '\([a-z-]\+\): \(.*\)' $INFILE | \ - sed -e's/\([a-z-]\+\): \(.*\)/\1/g') -for key in $VAR_KEYS; do - VARS[$key]=$(grep "$key:" $INFILE | \ - sed -e "s/$key: \(.*\)/\1/g") -done - -#echo -n Generating ${VARS[site-name]}: ${VARS[site-desc]}... - -grep -- '---' $INFILE 2>&1 >/dev/null \ - && DOC=$(sed '0,/---/d' $INFILE) \ - || DOC=$(cat $INFILE) - -CSSHREF=$CSSFILE -INDIR=$(dirname $INFILE 2>/dev/null) -[ "$INDIR" != "." ] && CSSHREF=../$CSSFILE -#echo CSSHREF=$CSSHREF >&2 - -if [ $BODY_ONLY -eq 0 ]; then - begin_html - begin_head - head - [ -f $CSSFILE ] && add_css $CSSHREF - end_head - begin_body -fi -body "$DOC" -if [ $BODY_ONLY -eq 0 ]; then - end_body - end_html -fi - -#echo done. diff --git a/slweb.1 b/slweb.1 @@ -1,125 +0,0 @@ -'\" -.\" Manpage for slweb(1) -. -.mso an-ext.tmac -. -.TH SLWEB "1" "11 Nov 2020" "slweb v0.1-beta" "General Commands Manual" -.SH NAME -slweb \- Simple static website generator -. -.SH SYNOPSIS -.SY slweb -.RI [ options ] -.RI [ filename ] -.YS -. -.SH COPYRIGHT -slweb Copyright \(co 2020 Strahinya Radich. -. -.SH DESCRIPTION -.B Slweb -is a static website generator which aims at being simplistic. It uses -.BR sed (1) -to transform a custom Markdown-like syntax into -.SM HTML. -. -.SH OPTIONS -. -.TP -.B \-b -.TQ -.B \-\-body\-only -.br -Only add the contents of the \fC<body>\fR tag, skipping \fC<html>\fR and -\fC<head>\fR. -. -.TP -.B \-d <\fIdirectory\fR> -.TQ -.B \-\-basedir <\fIdirectory\fR> -.br -Set the base directory as a reference point to normalize paths in local links -(the argument to -.B --relative-to -option for the -.BR realpath (1) -command). -. -.TP -.B \-h -.TQ -.B \-\-help -.br -Print this usage information screen. -. -.TP -.B \-v -.TQ -.B \-\-version -.br -Print program version and exit. -. -.SH "SEE ALSO" -.BR sed (1) -. -.SH EXAMPLES -. -.PP -GIven the file \fCindex.slw\fR in the current directory: -.EX -.in +4 -.sp -site-name: Test website -site-desc: My first website in slweb ---- - -{main} -# Hello world - -This is an _example_ of a statically generated HTML. - -{/main} -.in -4 -.sp -.EE -after using the command: -.EX -.in +4 -.sp -$ slweb index.slw > index.html -.in -4 -.sp -.EE -file \fCindex.html\fR contains: -.EX -.in +4 -.sp -<!DOCTYPE html> -<html lang="en"> -<head> - <title>Test website</title> - <meta charset="utf8" /> - <meta name="viewport" content="width=device-width, initial-scale=1" /> - <meta name="generator" content="slweb" /> -</head> -<body> - -<main> -<h1>Hello world</h1> - -<p>This is an <em>example</em> of a statically generated HTML.</p> - -</main> -</body> -</html> -.in -4 -.sp -.EE -. -.SH BUGS -. -.PP -Bugs can be reported using the Github issue tracker at: -.UR https://\:github.com/\:Strahinja/\:slweb/\:issues -.UE - diff --git a/slweb.1.in b/slweb.1.in @@ -0,0 +1,191 @@ +'\" +.\" Manpage for slweb(1) +. +.mso an-ext.tmac +. +.TH SLWEB "1" "11 Nov 2020" "slweb %VERSION%" "General Commands Manual" +.SH NAME +slweb \- Simple static website generator +. +.SH SYNOPSIS +.SY slweb +.RI [ options ] +.RI [ filename ] +.YS +. +.SH COPYRIGHT +slweb Copyright \(co 2020 Strahinya Radich. +. +.SH DESCRIPTION +.B Slweb +is a static website generator which aims at being simplistic. It uses +.BR sed (1) +to transform a custom Markdown-like syntax into +.SM HTML. +. +.SH OPTIONS +. +.TP +.B \-b +.TQ +.B \-\-body\-only +.br +Only add the contents of the \fC<body>\fR tag, skipping \fC<html>\fR and +\fC<head>\fR. +. +.TP +.B \-d <\fIdirectory\fR> +.TQ +.B \-\-basedir <\fIdirectory\fR> +.br +Set the base directory as a reference point to normalize paths in local links +(the argument to +.B --relative-to +option for the +.BR realpath (1) +command). +. +.TP +.B \-h +.TQ +.B \-\-help +.br +Print this usage information screen. +. +.TP +.B \-v +.TQ +.B \-\-version +.br +Print program version and exit. +. +.SH REFERENCE +. +.PP +Files processed by +.B slweb +are using a minimal subset of Markdown with added directives. Supported +.B "Markdown features" +are: +.IP \[bu] 4 +.BR Paragraphs . +Text surrounded by newlines will be put inside \fC<p></p>\fR tags. +. +.IP \[bu] +.BR Headings . +A line starting with \fC#\fR followed by space will be put inside +\fC<h?></h?>\fR, where \fC?\fR stands for 1-4, depending on the number of +hashtags. +. +.IP \[bu] +.BR "Bold/italic (limited)" . +Text inside \fC*asterisks*\fR or \fC_underlines_\fR will be put inside +\fC<em></em>\fR. Text inside \fC**double asterisks**\fR will be put inside +\fC<strong></strong>\fR. +. +.IP \[bu] +.BR Backticks . +Text inside \fC`backticks`\fR will be put inside \fC<code></code>\fR. +. +.IP \[bu] +.BR "Links (limited)" . +The construct \fC[A link](https://example.com)\fR will be converted into \fC<a +href="https://example.com">A link</a>\fR. +. +.PP +.BR "Directives " include: +. +.IP \[bu] 4 +.BR "Class divs" . +Directive \fC{.myclass}{/.myclass}\fR will be transformed into \fC<div +class="myclass"></div>\fR. +. +.IP \[bu] +.BR "Id divs" . +Directive \fC{#myid}{/#myid}\fR will be transformed into \fC<div +id="myid"></div>\fR. +. +.IP \[bu] +.BR "General tags" . +Directive \fC{sometag}{/sometag}\fR will be transformed into +\fC<sometag></sometag>\fR. +. +.IP \[bu] +.BR Includes . +Directive \fC{include "somefile"}\fR will run \fCslweb -b\fR on +.I somefile.slw +(related to the directory containing the file containing the directive) and +include the resulting +.SM HTML. +. +.IP \[bu] +.BR "Brand \[lq]watermark\[rq]" . +Directive \fC{made-by}\fR is converted into a div containing the +\[lq]watermark\[rq] text and a link to the +.B slweb +home page. +. +.SH "SEE ALSO" +.BR sed (1) +. +.SH EXAMPLES +. +.PP +GIven the file \fCindex.slw\fR in the current directory: +.EX +.in +4 +.sp +site-name: Test website +site-desc: My first website in slweb +--- + +{main} +# Hello world + +This is an _example_ of a statically generated HTML. + +{/main} +.in -4 +.sp +.EE +after using the command: +.EX +.in +4 +.sp +$ slweb index.slw > index.html +.in -4 +.sp +.EE +file \fCindex.html\fR contains: +.EX +.in +4 +.sp +<!DOCTYPE html> +<html lang="en"> +<head> + <title>Test website</title> + <meta charset="utf8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <meta name="generator" content="slweb" /> +</head> +<body> + +<main> +<h1>Hello world</h1> + +<p>This is an <em>example</em> of a statically generated HTML.</p> + +</main> +</body> +</html> +.in -4 +.sp +.EE +. +.SH BUGS +. +.PP +Bugs can be reported using the Github issue tracker at: +.UR https://\:github.com/\:Strahinja/\:slweb/\:issues +.UE + diff --git a/slweb.in b/slweb.in @@ -0,0 +1,189 @@ +#!/bin/bash + +BODY_ONLY=0 +CSSFILE=index.css +INFILE=index.slw +OUTFILE=index.html +VERSION=%VERSION% + +function throw() +{ + echo $1 1>&2 + exit 1 +} + +function version() +{ + cat <<EOT +Slweb $VERSION +EOT + return 1 +} + +function usage() +{ + cat <<EOT +usage: $(basename $0) [-b|--body-only] [-d <dir>|--basedir <dir>] [-h|--help] [-v|--version] <file> +EOT + return 1 +} + +function begin_html() +{ +cat <<EOT +<!DOCTYPE html> +<html lang="en"> +EOT +} + +function end_html() +{ +cat <<EOT +</html> +EOT +} + +function begin_head() +{ +cat <<EOT +<head> +EOT +} + +function head() +{ +cat <<EOT + <title>${VARS[site-name]}</title> + <meta charset="utf8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <meta name="generator" content="slweb" /> +EOT +} + +function add_css() +{ + cat <<EOT + <link rel="stylesheet" href="$1" /> +EOT +} + +function end_head() +{ +cat <<EOT +</head> +EOT +} + +function begin_body() +{ +cat <<EOT +<body> +EOT +} + +function body() +{ + BODY=$(echo "$1" | \ + sed -e's/#### \([^\n]*\)$/<h4>\1<\/h4>/g' \ + -e's/### \([^\n]*\)$/<h3>\1<\/h3>/g' \ + -e's/## \([^\n]*\)$/<h2>\1<\/h2>/g' \ + -e's/# \([^\n]*\)$/<h1>\1<\/h1>/g' \ + -e 's/\*\*\([^*]\+\)\*\*/<strong>\1<\/strong>/g' \ + -e 's/\*\([^*]\+\)\*/<em>\1<\/em>/g' \ + -e 's/_\([^_]\+\)_/<em>\1<\/em>/g' \ + -e 's/`\([^`]\+\)`/<code>\1<\/code>/g' \ + -e 's/{\.\(.*\)}/<div class="\1">/' \ + -e 's/{\/\.\(.*\)}/<\/div><!--\1-->/' \ + -e 's/{#\(.*\)}/<div id="\1">/' \ + -e 's/{\/#\(.*\)}/<\/div><!--\1-->/' \ + -e "s/{made-by}/$MADEBY/g" \ + -e 's/{\([a-z-]\+\)}/<\1>/g' \ + -e 's/{\/\([a-z-]\+\)}/<\/\1>/g' \ + -e "s/\\[\\([^]]\\+\\)\\](\\([^)]\\+\\))/<a href=\"$(realpath --relative-to=$BASEDIR $(dirname $INFILE) | sed -e's/\//\\\//g')\/\\2\">\\1<\\/a>/" | \ + sed -e '1h;2,$H;$!d;g;s/\n\n\([^<{\n]\+\)/\n\n<p>\1/g' | \ + sed -e '1h;2,$H;$!d;g;s/\([^>}\n]\)\n\n/\1<\/p>\n\n/g' | \ + sed -e '1h;2,$H;$!d;g;s/\([^>}\n]\)$/\1<\/p>\n\n/g') + + INCLUDE=$(echo "$BODY" | \ + sed -e'/{include/s/.*{include \"\([.\/a-z0-9_ -]\+\)\"}.*/\1/;t;d' | \ + sed 1q) + + while [ ! -z "$INCLUDE" ]; do + INCLUDE_BODY=$($0 -b -d $(dirname $INFILE) "$(dirname $INFILE)/$INCLUDE.slw") + BODY=$(echo "$BODY" | \ + sed -e"s/{include \\\"$(echo $INCLUDE | sed -e's/\//\\\//g')\\\"}/$(echo $INCLUDE_BODY | sed -e's/\//\\\//g')/") + INCLUDE=$(echo "$BODY" | \ + sed -e'/{include/s/.*{include \"\([.\/a-z0-9_ -]\+\)\"}.*/\1/;t;d' | \ + sed 1q) + done + echo "$BODY" +} + +function end_body() +{ +cat <<EOT +</body> +EOT +} + +BASEDIR=. +MADEBY=$(cat <<EOT | sed -e's/\//\\\//g' -e"s/\"/\\\\\"/g" | sed -z -e's/\n/\\n/g' +<div id="made-by"> +Generated by <a href="https://github.com/Strahinja/slweb">slweb</a> +© 2020 Strahinya Radich. +</div><!--made-by--> +EOT +) + +while [ $# -gt 0 ]; do + case "$1" in + -v|--version) version; exit + ;; + -h|--help) usage; exit + ;; + -b|--body-only) BODY_ONLY=1 + ;; + -d|--basedir) shift; BASEDIR=$1 + ;; + *) INFILE=$1 + ;; + esac + shift +done + +[ -f $INFILE ] || throw "$INFILE not present" + +declare -A VARS +VAR_KEYS=$(grep '\([a-z-]\+\): \(.*\)' $INFILE | \ + sed -e's/\([a-z-]\+\): \(.*\)/\1/g') +for key in $VAR_KEYS; do + VARS[$key]=$(grep "$key:" $INFILE | \ + sed -e "s/$key: \(.*\)/\1/g") +done + +#echo -n Generating ${VARS[site-name]}: ${VARS[site-desc]}... + +grep -- '---' $INFILE 2>&1 >/dev/null \ + && DOC=$(sed '0,/---/d' $INFILE) \ + || DOC=$(cat $INFILE) + +CSSHREF=$CSSFILE +INDIR=$(dirname $INFILE 2>/dev/null) +[ "$INDIR" != "." ] && CSSHREF=$CSSFILE +#echo CSSHREF=$CSSHREF >&2 + +if [ $BODY_ONLY -eq 0 ]; then + begin_html + begin_head + head + [ -f $INDIR/$CSSFILE ] && add_css $CSSHREF + end_head + begin_body +fi +body "$DOC" +if [ $BODY_ONLY -eq 0 ]; then + end_body + end_html +fi + +#echo done.