чување 1f14b1599dabe98e093fec5f16cd3f7828af5f65
родитељ edf38bc5c8699804f399846241391d31e2a66293
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Thu, 10 Dec 2020 14:22:54 +0100
Beginning of a migration to C
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | Makefile | | | 22 | +++++++++++----------- |
| A | defs.h | | | 52 | ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | index.html | | | 114 | +------------------------------------------------------------------------------ |
| A | slweb.c | | | 184 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
измењених датотека: 4, додавања: 248(+), брисања: 124(-)
diff --git a/Makefile b/Makefile
@@ -1,14 +1,16 @@
-SLWVERSION = v0.2.8-beta
+SLWVERSION = v0.3.0
SLWDATE = 10 Dec 2020
BINDIR = /usr/local/bin
MANDIR = /usr/local/share/man/man1
+CFLAGS=
+LIBS=-lunistring
BROWSER = surf
EXAMPLE_PAGES = $(addsuffix /index.slw,examples/basic examples/includes examples/tags)
#EXAMPLE_PAGES += $(addprefix examples/includes,
EXAMPLE_PAGES_HTML = $(EXAMPLE_PAGES:.slw=.html)
%.html: %.slw
- ./slweb $^ > $@
+ ./slweb $< > $@
%.gz: %
gzip -kf $<
@@ -23,24 +25,22 @@ EXAMPLE_PAGES_HTML = $(EXAMPLE_PAGES:.slw=.html)
cat $< | sed -e "s/%VERSION%/${SLWVERSION}/g" \
-e "s/%DATE%/${SLWDATE}/g" > $@
-all: in examples doc github-io
+all: in examples doc index.html
in: slweb slweb.1
-slweb: slweb.in
- cat $< | sed -e "s/%VERSION%/${SLWVERSION}/g" \
- -e "s/%DATE%/${SLWDATE}/g" > $@
- chmod 0755 slweb
+index.html: slweb index.slw
-examples: ${EXAMPLE_PAGES_HTML}
+slweb: slweb.o
+ $(CC) $(CFLAGS) $(LIBS) -o $@ $<
+
+examples: ${EXAMPLE_PAGES_HTML} slweb
view: index.html
$(BROWSER) index.html
doc: slweb.pdf slweb.1.gz
-github-io: index.html
-
install: all
mkdir -p $(BINDIR) $(MANDIR)
cp slweb $(BINDIR)
@@ -51,7 +51,7 @@ uninstall:
rm -f $(BINDIR)/slweb $(MANDIR)/slweb.1.gz 2>/dev/null
clean:
- rm -f slweb.1.gz slweb.pdf index.html $(EXAMPLE_PAGES_HTML) slweb slweb.1 2>/dev/null
+ rm -f *.o slweb.1.gz slweb.pdf index.html $(EXAMPLE_PAGES_HTML) slweb slweb.1 2>/dev/null
rebuild: clean all
diff --git a/defs.h b/defs.h
@@ -0,0 +1,52 @@
+/*
+ * slweb - Simple static webpage generator
+ * Copyright (C) 2020 Страхиња Радић
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef __DEFS_H
+#define __DEFS_H
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistr.h>
+#include <unistdio.h>
+#include <uniwidth.h>
+
+#define PROGRAMNAME "slweb"
+#define VERSION "0.3.0-beta"
+
+#define BUFSIZE 1024
+
+typedef enum
+{
+ FALSE = 0,
+ TRUE = 1
+} BOOL;
+
+typedef enum
+{
+ CMD_NONE,
+ CMD_BODY_ONLY,
+ CMD_BASEDIR,
+ CMD_HELP,
+ CMD_VERSION
+} Command;
+
+#endif /* __DEFS_H */
+
diff --git a/index.html b/index.html
@@ -1,113 +1 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <title>slweb - Simple static website generator. </title>
- <meta charset="utf8" />
- <meta name="description" content="Simple static website generator" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <meta name="generator" content="slweb" />
- <link rel="stylesheet" href="index.css" />
-</head>
-<body>
-<main>
-<h1>slweb</h1>
-
-<p><strong>Slweb</strong> is a static website generator which aims at being simplistic. It uses
-<strong>sed</strong>(1) to transform a custom Markdown-like syntax into HTML.</p>
-
-<h2>Install</h2>
-
-<pre>
-$ git clone https://github.com/Strahinja/slweb.git
-$ cd slweb
-$ make && sudo make install
-</pre>
-
-
-
-<h2>Examples</h2>
-
-<p>See the <code><a href="https://github.com/Strahinja/slweb/tree/master/examples">examples/</a></code> directory in this repository.</p>
-
-<p>Given the file <code>index.slw</code> in the current directory:</p>
-
-<pre>
-site-name: Test website
-site-desc: My first website in slweb
----
-
-{main}
-# Hello world
-
-This is an _example_ of a statically generated HTML.
-
-{/main}
-</pre>
-
-
-
-<p>after using the command:</p>
-
-<pre>
-$ slweb index.slw > index.html
-</pre>
-
-
-
-<p>file <code>index.html</code> contains:</p>
-
-<pre>
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <title>Test website</title>
- <meta charset="utf8" />
- <meta name="description" content="My first website in slweb" />
- <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>
-</pre>
-
-
-
-<h2>License</h2>
-
-<p>slweb - Simple static website generator.<br />
-Copyright (C) 2020 Страхиња Радић</p>
-
-<p>This program is free software: you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free Software
-Foundation, either version 3 of the License, or (at your option) any later
-version. </p>
-
-<p>This program is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
-PARTICULAR PURPOSE. See the GNU General Public License for more details. </p>
-
-<p>You should have received a copy of the GNU General Public License along with
-this program. If not, see <https://www.gnu.org/licenses/>. </p>
-
-<div id="git-log">
-Previous commit:
-index.slw 03a4ff6 2020-12-03 19:57:22 +0100 (Страхиња Радић) (tag: v0.2.5-beta)
-</div><!--git-log-->
-
-
-<div id="made-by">
-Generated by <a href="https://github.com/Strahinja/slweb">slweb</a>
-© 2020 Strahinya Radich.
-</div><!--made-by-->
-
-</main>
-</body>
-</html>
+debug: basedir=., filename=index.slw, body_only=FALSE
diff --git a/slweb.c b/slweb.c
@@ -0,0 +1,184 @@
+/*
+ * slweb - Simple static webpage generator
+ * Copyright (C) 2020 Страхиња Радић
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "defs.h"
+
+int
+version()
+{
+ printf("%s v%s\n", PROGRAMNAME, VERSION);
+ return 0;
+}
+
+int
+usage()
+{
+ printf("Usage: %s [-b|--body-only] [-d|--basedir <dir>] [-h|--help]"
+ " [-v|--version] [filename]\n", PROGRAMNAME);
+ return 0;
+}
+
+int
+error(int code, uint8_t* fmt, ...)
+{
+ uint8_t buf[BUFSIZE];
+ va_list args;
+ va_start(args, fmt);
+ u8_vsnprintf(buf, sizeof(buf), fmt, args);
+ va_end(args);
+ fprintf(stderr, "%s: %s", PROGRAMNAME, buf);
+ return code;
+}
+
+char*
+substr(char* src, int start, int finish)
+{
+ int len = strlen(src);
+ if (finish > len)
+ finish = len;
+ int substr_len = finish-start;
+ if (substr_len < 0)
+ substr_len = 0;
+ char* result = (char*) calloc(substr_len+1, sizeof(char));
+ char* presult = result;
+
+ for (int i = start; i < finish && *(src+i) != '\0'; i++)
+ {
+ *presult++ = *(src+i);
+ }
+ *presult = '\0';
+
+ return result;
+}
+
+int
+set_basedir(char* arg, char** basedir)
+{
+ if (*basedir)
+ free(*basedir);
+
+ *basedir = (char*) calloc(strlen(arg)+1, sizeof(char));
+
+ if (!*basedir)
+ return 1;
+
+ strcpy(*basedir, arg);
+
+ return 0;
+}
+
+int
+main(int argc, char** argv)
+{
+ char* arg;
+ Command cmd = CMD_NONE;
+ char* filename = NULL;
+ BOOL body_only = FALSE;
+ char* basedir = NULL;
+
+ basedir = (char*) calloc(2, sizeof(char));
+ basedir[0] = '.';
+
+ while ((arg = *++argv))
+ {
+ if (*arg == '-')
+ {
+ arg++;
+ char c = *arg++;
+ int result;
+
+ if (c == '-')
+ {
+ if (!strcmp(arg, "version"))
+ {
+ cmd = CMD_VERSION;
+ }
+ else if (!strcmp(arg, "body-only"))
+ {
+ arg += strlen("body-only");
+ body_only = TRUE;
+ }
+ else if (!strcmp(substr(arg, 0, strlen("basedir")), "basedir"))
+ {
+ arg += strlen("basedir");
+ result = set_basedir(arg, &basedir);
+ if (result)
+ return result;
+ }
+ else if (!strcmp(arg, "help"))
+ {
+ return usage();
+ }
+ else
+ {
+ error(1, (uint8_t*)"Invalid argument: --%s\n", arg);
+ return usage();
+ }
+ }
+ else
+ {
+ switch (c)
+ {
+ case 'b':
+ body_only = TRUE;
+ break;
+ case 'd':
+ cmd = CMD_BASEDIR;
+ break;
+ case 'h':
+ return usage();
+ break;
+ case 'v':
+ cmd = CMD_VERSION;
+ break;
+ default:
+ error(1, (uint8_t*)"Invalid argument: -%c\n", c);
+ return usage();
+ }
+ }
+ }
+ else
+ {
+ int result;
+
+ if (cmd == CMD_BASEDIR)
+ {
+ result = set_basedir(arg, &basedir);
+ if (result)
+ return result;
+ }
+ else
+ {
+ filename = arg;
+ }
+ cmd = CMD_NONE;
+ }
+ }
+
+ if (cmd == CMD_VERSION)
+ return version();
+
+ /*
+ *printf("debug: basedir=%s, filename=%s, body_only=%s\n",
+ * basedir, filename ? filename : "(NULL)", body_only ? "TRUE" : "FALSE");
+ */
+
+ return 0;
+}
+