чување 0bfc85afbb6942543638118c36f25c0f36d7a919
родитељ 1f14b1599dabe98e093fec5f16cd3f7828af5f65
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Thu, 10 Dec 2020 21:37:27 +0100
Continued WIP migration to C
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 5, додавања: 134(+), брисања: 3(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,4 @@
+.cache
*~
*.bak
*.orig
@@ -10,6 +11,7 @@ nohup.out
*.gz
slweb
slweb.1
+compile_commands.json
examples/basic/index.html
examples/includes/index.html
examples/tags/index.html
diff --git a/Makefile b/Makefile
@@ -2,7 +2,7 @@ SLWVERSION = v0.3.0
SLWDATE = 10 Dec 2020
BINDIR = /usr/local/bin
MANDIR = /usr/local/share/man/man1
-CFLAGS=
+CFLAGS=-g
LIBS=-lunistring
BROWSER = surf
EXAMPLE_PAGES = $(addsuffix /index.slw,examples/basic examples/includes examples/tags)
diff --git a/defs.h b/defs.h
@@ -20,6 +20,7 @@
#ifndef __DEFS_H
#define __DEFS_H
+#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
diff --git a/index.html b/index.html
@@ -1 +1,89 @@
-debug: basedir=., filename=index.slw, body_only=FALSE
+site-name: slweb - Simple static website generator.
+site-desc: Simple static website generator
+stylesheet: index.css
+---
+{main}
+# slweb
+
+**Slweb** is a static website generator which aims at being simplistic. It uses
+**sed**(1) to transform a custom Markdown-like syntax into HTML.
+
+## Install
+
+```
+$ git clone https://github.com/Strahinja/slweb.git
+$ cd slweb
+$ make && sudo make install
+```
+
+## Examples
+
+See the `[examples/](https://github.com/Strahinja/slweb/tree/master/examples)` directory in this repository.
+
+Given the file `index.slw` in the current directory:
+
+```
+site-name: Test website
+site-desc: My first website in slweb
+---
+\
+\{main}
+\# Hello world
+\
+This is an \_example_ of a statically generated HTML.
+\
+\{/main}
+```
+
+after using the command:
+
+```
+$ slweb index.slw > index.html
+```
+
+file `index.html` contains:
+
+```
+<!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>
+```
+
+## License
+
+slweb - Simple static website 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/>.
+
+{git-log}
+
+{made-by}
+{/main}
+
diff --git a/slweb.c b/slweb.c
@@ -18,6 +18,7 @@
*/
#include "defs.h"
+#include <stdlib.h>
int
version()
@@ -40,7 +41,7 @@ error(int code, uint8_t* fmt, ...)
uint8_t buf[BUFSIZE];
va_list args;
va_start(args, fmt);
- u8_vsnprintf(buf, sizeof(buf), fmt, args);
+ u8_vsnprintf(buf, sizeof(buf), (const char*)fmt, args);
va_end(args);
fprintf(stderr, "%s: %s", PROGRAMNAME, buf);
return code;
@@ -73,6 +74,11 @@ set_basedir(char* arg, char** basedir)
if (*basedir)
free(*basedir);
+ if (strlen(arg) < 1)
+ {
+ return error(1, (uint8_t*)"--basedir: Argument required\n");
+ }
+
*basedir = (char*) calloc(strlen(arg)+1, sizeof(char));
if (!*basedir)
@@ -171,6 +177,9 @@ main(int argc, char** argv)
}
}
+ if (cmd == CMD_BASEDIR)
+ return error(1, (uint8_t*)"-d: Argument required\n");
+
if (cmd == CMD_VERSION)
return version();
@@ -179,6 +188,37 @@ main(int argc, char** argv)
* basedir, filename ? filename : "(NULL)", body_only ? "TRUE" : "FALSE");
*/
+ FILE* input = NULL;
+ if (filename)
+ {
+ input = fopen(filename, "r");
+ if (!input)
+ {
+ return error(ENOENT, (uint8_t*)"File not found: %s\n", filename);
+ }
+ }
+ else
+ input = stdin;
+
+ uint8_t* line = (uint8_t*) calloc(BUFSIZE, sizeof(uint8_t));
+ size_t line_len = 0;
+
+ do
+ {
+ fgets((char*)line, sizeof(uint8_t) * BUFSIZE, input);
+ line_len = u8_strlen(line);
+
+ if (!feof(input))
+ {
+ uint8_t* eol = u8_strchr(line, (ucs4_t)'\n');
+ if (eol)
+ *eol = (uint8_t)'\0';
+
+ printf("%s\n", line);
+ }
+ }
+ while (!feof(input));
+
return 0;
}