diff --git a/blog b/blog index e260329..4198bdd 100755 --- a/blog +++ b/blog @@ -25,6 +25,9 @@ my IO::Path:D $default-db-dir = #| The default output directory my IO::Path:D $default-site-dir = $default-blog-dir.add('site/'); +#| The default idris ipkg +my IO::Path:D $default-ipkg = $default-blog-dir.add('projects/Idris/Idris.ipkg'); + #| Initalize the database multi MAIN( "db", @@ -92,6 +95,8 @@ multi MAIN( "idris", #| The path to the idris file IO::Path(Str) $source, + #| The path to the ipkg file + IO::Path(Str) :$ipkg = $default-ipkg, #| The path of the database file IO::Path(Str) :$db-dir = $default-db-dir, #| The date/time the post should be recorded as posted at @@ -106,6 +111,7 @@ multi MAIN( source => $source.absolute.IO, posted-at => $posted-at, hidden => $hidden, + ipkg => $ipkg.absolute.IO, ); $db.write: $db-dir; say 'Post inserted with id ', $id; diff --git a/lib/Config.rakumod b/lib/Config.rakumod index fc1f057..5503bbb 100644 --- a/lib/Config.rakumod +++ b/lib/Config.rakumod @@ -2,21 +2,65 @@ use v6.e.PREVIEW; use HTML::Functional; use DB::BlogMeta; +use DB::Post; unit class Config; -method generate-post(Str:D $title, Str:D $content, BlogMeta:D $meta) { - my $head = +method generate-head(Str:D $title, BlogMeta:D $meta, $description?) { head [ meta :charset; meta :name, :content; - # meta :name, :content; - title ""; + meta :author :content; + title "{$meta.title} — $title"; + # Add description, if one exists + do if $description ~~ Str:D { + meta :description :content($description) + } else { + [] + } + # Preconnect to all our resource sources + link :rel :href; + link :rel :href; + link :rel :href :crossorigin; + # Load fonts, Iosevka Alie for code, and Open Sans for content + link :rel, + :href; + link :rel, + :href; + # Inline our style sheets + style %?RESOURCES.slurp; + style %?RESOURCES.slurp; ]; - my $body; - # my $footer; +} - die "Not done yet"; +method site-header(BlogMeta:D $meta) { + header :class, [ + div :class, [ + # TODO: Use a real image here + $meta.title + ]; + div :class, [ + $meta.tagline + ]; + div :class, [ + + ] + ] +} + +# TODO: Support GFM admonitions +method generate-post(Post:D $post, BlogMeta:D $meta) { + my $content = $post.render-html; + my $head = self.generate-head($post.title, $meta, $post.description); + my $body = + body [ + self.site-header: $meta; + div :class, [ + $content + ] + ]; + # TODO: Setup footer + # my $footer; my $html = html :lang, [ $head, diff --git a/lib/DB.rakumod b/lib/DB.rakumod index 55ab136..fd95f6f 100644 --- a/lib/DB.rakumod +++ b/lib/DB.rakumod @@ -78,13 +78,14 @@ class PostDB { mkdir $by-slug unless $by-slug.e; # Render all the posts and make symlinks for %!posts.kv -> $id, $post { - my $html = - $config.generate-post: - $post.title, $post.render-html, $!meta; + my $html = $config.generate-post: $post, $!meta; my $id-path = $by-id.add: "$id.html"; $id-path.spurt: $html; for $post.all-slugs -> $slug { - $by-slug.add($slug).symlink: $id-path; + # remove the symlink if it already exists + my $slug-path = $by-slug.add: $slug; + $slug-path.unlink if $slug-path.l; + $id-path.symlink: $slug-path; } } # Render the archive diff --git a/lib/DB/IdrisPost.rakumod b/lib/DB/IdrisPost.rakumod index 5dd6f9a..1e229ac 100644 --- a/lib/DB/IdrisPost.rakumod +++ b/lib/DB/IdrisPost.rakumod @@ -2,6 +2,7 @@ use v6.e.PREVIEW; use Pandoc; use JSON::Class:auth; +use File::Temp; use DB::Post; @@ -11,7 +12,18 @@ unit class IdrisPost does Post is json(:pretty); #| Marker for disambiguation between post types in json representation, the #| cheaty way -has Bool:D $.idris = True; +has Bool:D $.idris is json = True; + +#| Override the generated description for this post +has Str $.summary is json; + +#| Location of the ipkg for the package containing the post +has IO::Path:D $.ipkg + is required + is json( + :to-json(*.Str), + :from-json(*.IO) + ); method title(--> Str:D) { markdown-title($!source) @@ -20,5 +32,65 @@ method title(--> Str:D) { # Use katla to highlight our file, mangle the resulting output, and then pass it # through to pandoc for html generation method render-html(--> Str:D) { - die "Not implemented"; + my $project-dir = $!ipkg.parent; + indir $project-dir, { + # Do a pack build to make sure we have the needed files + # TODO: Figure out how to only do this once + pack ; + # First we have to find the location of the ttm file + my $relative-path = $!source.extension('ttm').relative: + $project-dir.add('src'); + my $ttc-dir = dir($project-dir.add('build/ttc/'))[0]; + my $ttm-path = $ttc-dir.add: $relative-path; + # Run through katla + my $output = katla 'markdown', $!source, $ttm-path; + # Send output to pandoc through a temporary file + my ($filename, $filehandle) = tempfile; + $filehandle.spurt: $output, :close; + markdown-to-html $filename.IO + }; +} + +# Return our summary, if we have one, otherwise extract the first paragraph of +# the markdown document +method description(--> Str) { + if $!summary { + $!summary + } else { + markdown-first-paragraph $!source + } +} + +# Run a pack command, erroring on failure +sub pack(*@args) { + my $pack = run 'pack', @args, :out, :err; + my $pack-out = $pack.out.slurp: :close; + my $pack-err = $pack.err.slurp: :close; + die "Pack exited with {$pack.exitcode}\nout: $pack-out\nerr: $pack-err" + unless $pack; +} + +# Run a katla command, erroring on failure, returning the standard out, and +# mangling the output accordingly +sub katla(*@args --> Str:D) { + # Run the requested katla command + my $katla = run 'katla', @args, :out, :err; + my $katla-out = $katla.out.slurp: :close; + my $katla-err = $katla.err.slurp: :close; + die "Katla exited with {$katla.exitcode}\nout: $katla-out\nerr: $katla-err" + unless $katla; + # Modify the output to use our styling instead of katla's + # Remove the style block and line breaks + $katla-out ~~ s:g/''//; + $katla-out ~~ s:g/'
'//; + # Use
 blocks for the code and inject our own class for idris code
+    $katla-out ~~ s:g/'' \s*/
/;
+    $katla-out ~~ s:g/\s* ''/<\/code><\/pre>/;
+    # Replace the highlighting classes with our own
+    $katla-out ~~ s:g/'class="Idris' (\w+) '"'/class="hl-{$/[0].lc}"/;
+    # Clean up inappropiate escaping of some characters
+    $katla-out ~~ s:g/'\\*'/*/;
+    $katla-out ~~ s:g/'\\_'/_/;
+    $katla-out ~~ s:g/'\\\\'/\\/;
+    $katla-out
 }
diff --git a/lib/DB/MarkdownPost.rakumod b/lib/DB/MarkdownPost.rakumod
index e395949..0f189d5 100644
--- a/lib/DB/MarkdownPost.rakumod
+++ b/lib/DB/MarkdownPost.rakumod
@@ -2,7 +2,6 @@ use v6.e.PREVIEW;
 
 use Pandoc;
 use JSON::Class:auth;
-
 use DB::Post;
 
 #| A plain markdown post
@@ -12,11 +11,24 @@ unit class MarkdownPost does Post is json(:pretty);
 #| cheaty way
 has Bool:D $.markdown = True;
 
+#| Override the generated description for this post
+has Str $.summary;
+
 method title(--> Str:D) {
-    markdown-title($!source)
+    markdown-title $!source
 }
 
 # Simply provide our source file to pandoc
 method render-html(--> Str:D) {
-    die "Not implemented";
+    markdown-to-html $!source
+}
+
+# Return our summary, if we have one, otherwise extract the first paragraph of
+# the markdown document
+method description(--> Str) {
+    if $!summary {
+        $!summary
+    } else {
+       markdown-first-paragraph $!source
+    }
 }
diff --git a/lib/DB/Post.rakumod b/lib/DB/Post.rakumod
index f27c2b6..0c43fdb 100644
--- a/lib/DB/Post.rakumod
+++ b/lib/DB/Post.rakumod
@@ -53,3 +53,8 @@ method all-slugs(--> Array[Str:D]) {
 
 #| Render this post to an html body
 method render-html(--> Str:D) {...}
+
+#| Get the description for this post, returning nil if there is none
+method description(--> Str) {
+    Nil
+}
diff --git a/lib/Pandoc.rakumod b/lib/Pandoc.rakumod
index fb80e4e..74048f0 100644
--- a/lib/Pandoc.rakumod
+++ b/lib/Pandoc.rakumod
@@ -3,13 +3,10 @@ unit module Pandoc;
 
 use JSON::Fast;
 
-#| Extract the title from a markdown document
-#|
-#| The title is the only top level header, will throw an error if there are
-#| multiple top level headers or are none
-sub markdown-title(IO::Path:D $file --> Str:D) is export {
+#| Run pandoc with the given arguments, dieing on failure
+sub pandoc(*@args --> Str:D) {
     # Call into pandoc
-    my $pandoc = run , $file, :out, :err;
+    my $pandoc = run 'pandoc', @args, :out, :err;
 
     # Collect the output
     my $output = $pandoc.out.slurp: :close;
@@ -17,6 +14,17 @@ sub markdown-title(IO::Path:D $file --> Str:D) is export {
     die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr"
     unless $pandoc;
 
+    $output
+}
+
+#| Extract the title from a markdown document
+#|
+#| The title is the only top level header, will throw an error if there are
+#| multiple top level headers or are none
+sub markdown-title(IO::Path:D $file --> Str:D) is export {
+    # Collect the output
+    my $output = pandoc <-f gfm -t JSON>, $file;
+
     # Parse out output from pandoc, we are making an executive decision to trust
     # pandoc here, so we won't do any error handling for pandoc's output
     my %parsed = from-json $output;
@@ -27,12 +35,8 @@ sub markdown-title(IO::Path:D $file --> Str:D) is export {
         $v ~~ Associative && $v ~~ "Header"
     }
     my @headers = %parsed.grep(&is-header).grep(*[0] == 1);
-    if @headers.elems > 1 {
-        die "More than one top level header in $file";
-    };
-    if @headers.elems == 0 {
-        die "No top level headers in $file";
-    };
+    die "More than one top level header in $file" if @headers.elems > 1;
+    die "No top level headers in $file" if @headers.elems == 0;
 
     # Extract the header and process it into a string
     my @header = @headers[0][2].flat;
@@ -55,15 +59,38 @@ sub markdown-title(IO::Path:D $file --> Str:D) is export {
     return $title;
 }
 
+#| Use pandoc to extract the first paragraph of a markdown document
+sub markdown-first-paragraph(IO::Path:D $file --> Str:D) is export {
+    my $output = pandoc <-f gfm -t JSON>, $file;
+    my %parsed = from-json $output;
+    # Extract a list of paragraphs from the pandoc output
+    my sub is-para($v) {
+        $v ~~ Associative && $v ~~ 'Para'
+    }
+    my @paras = %parsed.grep(&is-para);
+    die "No paragraphs in markdown" if @paras.elems == 0;
+    my @para = @paras[0][0].flat;
+    # Proces it into a string
+    my $para = "";
+    for @para -> $component {
+        next unless $component ~~ Associative;
+        given $component {
+            when "Str" {
+                $para ~= $component;
+            }
+            when "Space" {
+                $para ~= " ";
+            }
+            default {
+                die "Invalid component type: $_";
+            }
+        }
+    }
+
+    $para
+}
+
 #| Use pandoc to render a markdown document to html
 sub markdown-to-html(IO::Path:D $file --> Str:D) is export {
-    # Call into pandoc
-    my $pandoc = run , $file, :out, :err;
-    # Collect the output
-    my $output = $pandoc.out.slurp: :close;
-    my $stderr = $pandoc.err.slurp: :close;
-    die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr"
-    unless $pandoc;
-
-    $output
+    pandoc <-f gfm>, $file
 }
diff --git a/projects/Idris/src/Posts/HelloWorld.md b/projects/Idris/src/Posts/HelloWorld.md
index 026080b..afa7d4f 100644
--- a/projects/Idris/src/Posts/HelloWorld.md
+++ b/projects/Idris/src/Posts/HelloWorld.md
@@ -5,3 +5,33 @@ module Posts.HelloWorld
 
 %default total
 ```
+
+Here is an example function that prints to the standard out:
+
+
+function debug null byte compile syntax error protocol stack overflow cache memory leak parse integer void pointer exception handler runtime deploy code fragment database query optimize algorithm refactor git commit push merge conflict
+function debug null byte compile syntax error protocol stack overflow cache memory leak parse integer void pointer exception handler runtime deploy code fragment database query optimize algorithm refactor git commit push merge conflict
+function debug null byte compile syntax error protocol stack overflow cache memory leak parse integer void pointer exception handler runtime deploy code fragment database query optimize algorithm refactor git commit push merge conflict
+
+
+```idris
+main : IO ()
+main = do
+  putStrLn "Hello World"
+  putStrLn "Hello \n with new lines \n world"
+  putStrLn "And some other potential problem characters: * _"
+```
+
+And a function with some more stuff:
+
+```idris
+-- A regular comment
+
+||| A doc comment
+thingomizer : (a : String) -> IO ()
+thingomizer a = 
+  let xs = map (+ 1) [1, 2, 3, 4]
+  in do
+    putStrLn a
+    printLn xs
+```
diff --git a/projects/Markdown/MyNewBlog.md b/projects/Markdown/MyNewBlog.md
index 965b3f2..59e094b 100644
--- a/projects/Markdown/MyNewBlog.md
+++ b/projects/Markdown/MyNewBlog.md
@@ -1,3 +1,23 @@
 # My New Blog
 
 This is a blog that uses a static site generator written in raku!
+
+## A sub headings
+
+With some text under it
+
+### A third layer
+
+Even more text
+
+## Back up
+
+function debug null byte compile syntax error protocol stack overflow cache memory leak parse integer void pointer exception handler runtime deploy code fragment database query optimize algorithm refactor git commit push merge conflict
+
+### Back down
+
+here we go
+
+#### even further
+
+weeeeee
diff --git a/resources/code.css b/resources/code.css
new file mode 100644
index 0000000..4635ff2
--- /dev/null
+++ b/resources/code.css
@@ -0,0 +1,44 @@
+code {
+    font-family: "Iosevka Aile Web", monospace;
+    background-color: light-dark(#fbf3db, #103c48);
+    color: light-dark(#53676d, #adbcbc);
+    min-width: 80ch;
+    width: 80%;
+    display: block;
+    padding: 1rem;
+    border-radius: 0.55rem / 0.5rem;
+}
+
+pre {
+    display: flex;
+    align-items: center;
+    justify-content: center;
+}
+
+.hl-type {
+    color: light-dark(#ad8900, #dbb32d);
+}
+
+.hl-module {
+    font-style: italic;
+}
+
+.hl-function {
+    color: light-dark(#428b00, #84c747);
+}
+
+.hl-bound {
+    color: light-dark(#ca4898, #f275be);
+}
+
+.hl-keyword {
+    color: light-dark(#0072d4, #4695f7);
+}
+
+.hl-comment {
+    color: light-dark(#909995, #72898f);
+}
+
+.hl-data {
+    color: light-dark(#d2212d, #ed4a46)
+}
diff --git a/resources/main.css b/resources/main.css
new file mode 100644
index 0000000..602362d
--- /dev/null
+++ b/resources/main.css
@@ -0,0 +1,65 @@
+:root {
+    color-scheme: light dark;
+    color: light-dark(#474747, #b9b9b9);
+    background-color: light-dark(#ebebeb, #252525);
+    font-family: "Open Sans", sans-serif, serif;
+}
+
+body {
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    flex-direction: column;
+    gap: 1rem;
+}
+
+.site-header {
+    width: 60%;
+    display: block;
+    padding: 1rem;
+    border-radius: 1rem;
+    background-color: light-dark(#ffffff, #181818);
+    display: flex;
+    align-items: center;
+    flex-direction: column;
+}
+
+.site-logo {
+    color: light-dark(#d6000c, #ed4a46);
+}
+
+.site-tagline {
+}
+
+.post-body {
+    width: 66%;
+    display: block;
+    padding-left: 2rem;
+    padding-right: 2rem;
+    border-radius: 1rem;
+    background-color: light-dark(#ffffff, #181818);
+    /* text-align: justify; */
+}
+
+.post-body > h1 {
+    text-align: center;
+    color: light-dark(#dd0f9d, #eb6eb7);
+}
+
+.post-body > h2 {
+    text-align: center;
+    color: light-dark(#282828, #dedede);
+}
+
+.post-body > h3 {
+    text-align: center;
+    color: light-dark(#282828, #dedede);
+}
+
+.post-body > h4 {
+    text-align: center;
+    color: light-dark(#282828, #dedede);
+}
+.post-body > p {
+    text-indent: 2ch;
+}