diff --git a/.gitignore b/.gitignore index b799427..bbb65f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -blogtool \ No newline at end of file +blogtool +lib +shard.lock +testsite/dist \ No newline at end of file diff --git a/Makefile b/Makefile index 1865586..68d03ff 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,19 @@ clean: run: crystal run src/blogtool.cr -release: +release: dependencies crystal build --release --no-debug src/blogtool.cr -test: - cd testsite && crystal run ../src/blogtool.cr +dependencies: + shards install + +debug: + crystal build src/blogtool.cr + +test: debug + cd testsite && ../blogtool install: - install swayfocus $(PREFIX)/bin/ + install blogtool $(PREFIX)/bin/ + mkdir -p /etc/blogtool + install testsite/base.html /etc/blogtool/ \ No newline at end of file diff --git a/README.md b/README.md index 160dd0b..de63e36 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -## BLOGTOOL is a stupid simple static site generator +## BLOGTOOL is a non-bloated static site generator. -There are no themes or templates. You write a project structure like this: +There are no themes. You write a project structure like this: ``` myblog | @@ -10,11 +10,10 @@ myblog |-> assets | |-> article1.jpg | |-> photo.png -|-> profile.jpg |-> about.md |-> styles.css ``` -with utf-8 encoded articles. Blogtool generates a static site in dist/ with a list of articles for the home page, an about page with a profile picture and bio from profile.jpg and about.md, each page will include styless.css and all assets will be available at /static. +with utf-8 encoded articles. Blogtool generates a static site in dist/ with a list of articles for the home page, an about page from about.md, each page will include styless.css and all assets will be available at /static. Dashes (-) will be replaced with spaces in the index. ``` dist | @@ -22,11 +21,10 @@ dist |-> static | |-> article1.jpg | |-> photo.png -| |-> profile.jpg | |-> styles.css |-> article1.html |-> article2.html |-> about.html ``` -Invoke blogtool with `blogtool` after installing dependencies and adding the bin to your path. Run it in your project's root. There are no options. It will not remove old files from dist/. \ No newline at end of file +You can build blogtool by running `make && sudo make install`. Invoke blogtool by running `blogtool` in the project directory. There are no options. You can copy /etc/blogtool/base.html to the project root and make changes to overwrite the template. \ No newline at end of file diff --git a/blogtool b/blogtool index 8e3416a..b98bf08 100644 Binary files a/blogtool and b/blogtool differ diff --git a/src/blogtool.cr b/src/blogtool.cr index 901f99d..5a8aa5e 100644 --- a/src/blogtool.cr +++ b/src/blogtool.cr @@ -1,13 +1,56 @@ require "markd" require "dir" +require "file" +require "path" begin - Dir.mkdir "dist" + Dir.mkdir_p Path["dist/static"] rescue + if !Dir.exists? Path["dist/static"] + puts "Could not create required folders. Exiting." + exit 1 + end end -articles = Dir.entries "articles" -articles = articles.select { |i| i != "." && i != ".." } -puts articles +Base = begin + if File.file? "base.html" + File.read "base.html" + elsif File.file? Path[ENV["HOME"]+"/.config/blogtool/base.html"] + File.read Path[ENV["HOME"]+"/.config/blogtool/base.html"] + elsif File.file? Path["/etc/blogtool/base.html"] + File.read Path["/etc/blogtool/base.html"] + else + puts "Can't find base.html" + exit 1 + end +end + + +BaseCSS = begin + if File.file? Path[ENV["HOME"]+"/.config/blogtool/styles.css"] + File.read Path[ENV["HOME"]+"/.config/blogtool/styles.css"] + elsif File.file? Path["/etc/blogtool/styles.css"] + File.read Path["/etc/blogtool/styles.css"] + else + "" + end +end + +def render_file(filename : String | Path) + Base+Markd.to_html(File.read(filename), Markd::Options.new(smart: true, safe: false))+"" +end + +Dir.entries("articles").select{ |i| i != "." && i != ".." }.each do |article| + if article[article.size-3, article.size] == ".md" + File.write Path.new("dist", article[0,article.size-3]+".html"), render_file("articles/"+article) + else + File.write Path.new("dist", article+".html"), render_file("articles/"+article) + end +end + +Dir.entries("assets").select{ |i| i != "." && i != ".." }.each do |asset| + File.copy Path.new("assets", asset), Path.new("dist", "static", asset) +end -#print Markd.to_html(text, Markd::Options.new(smart: true, safe: false)); \ No newline at end of file +File.write Path.new("dist", "static", "styles.css"), BaseCSS+File.read("styles.css") +File.write Path.new("dist", "about.html"), render_file("about.md") diff --git a/testsite/profile.jpg b/testsite/assets/kitty.jpg similarity index 100% rename from testsite/profile.jpg rename to testsite/assets/kitty.jpg diff --git a/base.html b/testsite/base.html similarity index 100% rename from base.html rename to testsite/base.html diff --git a/testsite/dist/test.html b/testsite/dist/test.html index 9f8667b..a53710d 100644 --- a/testsite/dist/test.html +++ b/testsite/dist/test.html @@ -2,8 +2,7 @@ -

yeet

+

yeet

this is a test

weeeooo

- - \ No newline at end of file + \ No newline at end of file