Skip to content

Commit

Permalink
handle generation of normal page
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwill101 committed Nov 19, 2023
1 parent 67668a7 commit cfc7436
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 182 deletions.
33 changes: 33 additions & 0 deletions lib/content/content.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:gengen/utilities.dart';

class ContentTokenizer {
final RegExp _frontMatterExp = RegExp(
r'^\s*[+-]{3}\s*([\s\S]*?)\s[+-]{3}\s*\n*([\s\S]*)',
dotAll: false);

Content? parse(String content) {
var matches = _frontMatterExp.firstMatch(content);
if (matches != null && matches.groupCount >= 1) {
var first = matches.group(1);
var second = matches.group(2);
return Content(first, second);
}

return Content(null, content);
}
}

Content? toContent(String content) {
var tokenizer = ContentTokenizer();
var frontMatter = tokenizer.parse(content);
return frontMatter;
}

class Content {
String? _frontMatter;
String? content;

Content(this._frontMatter, this.content);

Map<String, dynamic> get frontMatter => getFrontMatter(_frontMatter ?? "");
}
68 changes: 45 additions & 23 deletions lib/generator/generator.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'dart:io';

import 'package:gengen/generator/pipeline.dart';
import 'package:gengen/generator/page/page.dart';
import 'package:gengen/generator/posts/post.dart';
import 'package:gengen/generator/static.dart';
import 'package:gengen/utilities.dart';
import 'package:gengen/pipeline/pipeline.dart';
import 'package:path/path.dart';

void gen({Directory? outputDir}) {
List<Pipeline<Generator>> pipelines = [
PostGenerator.pipeline(
PostGenerator(joinAll(["content", "posts"]), outputDir)),
PostGenerator.pipeline(PostGenerator("posts", outputDir)),
PageGenerator.pipeline(PageGenerator(current, outputDir)),
StaticGenerator.pipeline(StaticGenerator(joinAll(["static"]), outputDir))
];

Expand All @@ -19,6 +18,7 @@ void gen({Directory? outputDir}) {
}

abstract class Generator<T> {
static Map<String, Map<String, dynamic>> directoryDefaults = {};
String source;
List<String> sources = [];
List<T> collection = [];
Expand All @@ -27,39 +27,61 @@ abstract class Generator<T> {

Directory? destination;

List<String> ignoreDirs;

Generator(
{required this.source, this.destination, this.extensions = const []});
{required this.source,
this.destination,
this.ignoreDirs = const [],
this.extensions = const []});

Future<List<String>> _mapContent(String directory) async {
Directory root = Directory(directory);

if (!root.existsSync()) {
return [];
}

List<String> listing = [];
await root.list(recursive: true).forEach((FileSystemEntity entry) {
listing.add(entry.path);
var value = entry.path;

bool shouldSkip = false;

for (var blocked in this.ignoreDirs) {
if (value.startsWith(joinAll([current, blocked]))) {
shouldSkip = true;
break;
}
}

if (shouldSkip) {
return;
}

if (Directory(value).existsSync()) {
return;
}

String ext = basename(value).split(".").last;

if (!extensions.contains(ext)) {
return;
}

listing.add(value);
});

return listing;
}

Future<Generator<T>> collect() async {
await _collect();
sources = await _collect();
return this;
}

Future<List<String>> _collect() async {
var content = await _mapContent(source);
for (var path in content) {
if (await isDir(path)) {
continue;
}

String ext = basename(path).split(".").last;

if (!extensions.contains(ext)) {
continue;
}
sources.add(path);
}

return content;
return await _mapContent(source);
}

Future<Generator<T>> transform() async {
Expand Down
2 changes: 1 addition & 1 deletion lib/generator/handlers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:gengen/generator/generator.dart';
import 'package:gengen/generator/pipeline.dart';
import 'package:gengen/pipeline/pipeline.dart';

class CollectHandler<T> extends Handle<Generator<T>> {
@override
Expand Down
29 changes: 0 additions & 29 deletions lib/generator/page.dart

This file was deleted.

60 changes: 60 additions & 0 deletions lib/generator/page/page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:io';

import 'package:console/console.dart';
import 'package:gengen/content/content.dart';
import 'package:gengen/generator/generator.dart';
import 'package:gengen/generator/handlers.dart';
import 'package:gengen/generator/shared/handlers.dart';
import 'package:gengen/models.dart';
import 'package:gengen/pipeline/pipeline.dart';
import 'package:gengen/utilities.dart';

class PageGenerator extends Generator<Page> {
PageGenerator(String source, Directory? outputDir,
{super.extensions = const ["html", "liquid", "md", "markdown"],
super.ignoreDirs = const ["assets", "posts", "templates", "public"]})
: super(source: source, destination: outputDir);

@override
Future<PageGenerator> transform() async {
for (var source in sources) {
var fileContent = await readFile(source);
if (fileContent == null) {
continue;
}

var content = toContent(fileContent);
if (content == null) {
continue;
}

var directoryFrontMatter = getDirectoryFrontMatter(source) ?? {};

directoryFrontMatter.addAll({"permalink": PermalinkStructure.post});

var page = Page.fromYaml(content.frontMatter, source,
content.content ?? "", directoryFrontMatter, destination);

collection.add(page);
}
return this;
}

@override
Future<Generator<Page>> write() async {
Console.setTextColor(Color.WHITE.id, bright: true);

for (var item in collection) {
Pipeline<Base> pipeline = Pipeline(item, [LiquidWriter(), HtmlWriter()]);

pipeline.handle();
}

return this;
}

static Pipeline<Generator> pipeline(PageGenerator generator) {
return Pipeline<Generator>(
generator, [CollectHandler(), TransformHandler(), WriteHandler()]);
}
}
73 changes: 12 additions & 61 deletions lib/generator/posts/post.dart
Original file line number Diff line number Diff line change
@@ -1,73 +1,24 @@
import 'dart:io';

import 'package:console/console.dart';
import 'package:gengen/content/content.dart';
import 'package:gengen/generator/generator.dart';
import 'package:gengen/generator/handlers.dart';
import 'package:gengen/generator/pipeline.dart';
import 'package:gengen/generator/posts/handlers.dart';
import 'package:gengen/markdown/mardown.dart';
import 'package:gengen/generator/shared/handlers.dart';
import 'package:gengen/generator/shared/handlers.dart';
import 'package:gengen/models.dart';
import 'package:gengen/pipeline/pipeline.dart';
import 'package:gengen/utilities.dart';
import 'package:path/path.dart';
import 'package:toml/toml.dart';

class PostGenerator extends Generator<Post> {
Map<String, Map<String, dynamic>> directoryDefaults;

List<String> blockList = [];

PostGenerator(String source, Directory? outputDir,
{super.extensions = const ["md", "html"],
{super.extensions = const ["md", "markdown", "liquid", "html"],
this.blockList = const ["_index.md"],
Map<String, Map<String, dynamic>>? directoryDefaults})
: directoryDefaults = directoryDefaults ?? {},
super(source: source, destination: outputDir);

getFrontMatter(String matter) {
Map<String, dynamic> frontMatter = <String, dynamic>{};
try {
frontMatter = parseFrontMatter(matter).cast<String, dynamic>();
} catch (exc) {
try {
frontMatter = TomlDocument.parse(matter).toMap();
} catch (e) {
Console.setTextColor(Color.RED.id, bright: true);
print("[TOML] Unable to read front matter, giving up");
Console.setTextColor(Color.WHITE.id, bright: true);
return {};
}
}

return frontMatter;
}

Map<String, dynamic>? getDirectoryFrontMatter(String path) {
var dir = dirname(path);

if (directoryDefaults.containsKey(dir)) {
return directoryDefaults[dir];
}

directoryDefaults[dir] = {};

var index = File(joinAll([dir, "_index.md"]));

if (!index.existsSync()) {
return {};
}

var content = index.readAsStringSync();

var markdown = markdownContent(content);
if (markdown == null) {
return {};
}

var matter = getFrontMatter(markdown.frontmatter ?? "");
directoryDefaults[dir] = matter;

return matter;
}
: super(source: source, destination: outputDir);

@override
Future<PostGenerator> transform() async {
Expand All @@ -81,17 +32,17 @@ class PostGenerator extends Generator<Post> {
basename(path).startsWith(RegExp(r'^[-_*]'))) {
continue;
}
var markdown = markdownContent(fileContent);
if (markdown == null) {

var content = toContent(fileContent);
if (content == null) {
continue;
}

var contentFrontMatter = getFrontMatter(markdown.frontmatter ?? "");

var directoryFrontMatter = getDirectoryFrontMatter(path);

var post = Post.fromYaml(contentFrontMatter, path, markdown.content ?? "",
var post = Post.fromYaml(content.frontMatter, path, content.content ?? "",
directoryFrontMatter ?? {}, destination);

collection.add(post);
}
return this;
Expand All @@ -102,7 +53,7 @@ class PostGenerator extends Generator<Post> {
Console.setTextColor(Color.WHITE.id, bright: true);

for (var item in collection) {
Pipeline<Post> pipeline = Pipeline(item, [LiquidWriter(), HtmlWriter()]);
Pipeline<Base> pipeline = Pipeline(item, [LiquidWriter(), HtmlWriter()]);

pipeline.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@ import 'dart:async';
import 'dart:io';

import 'package:console/console.dart';
import 'package:gengen/generator/pipeline.dart';
import 'package:gengen/liquid/template.dart';
import 'package:gengen/models.dart';
import 'package:gengen/pipeline/pipeline.dart';
import 'package:markdown/markdown.dart';
import 'package:path/path.dart';

class HtmlWriter extends Handle<Post> {
class HtmlWriter extends Handle<Base> {
@override
void handle(Post data, HandleFunc<Post> next) {
void handle(Base data, HandleFunc<Base> next) {

var file = File(data.permalink());
var file = File(data.link());
file
.create(recursive: true)
.then((file) => file.writeAsString(markdownToHtml(data.content)))
.then((value) {
Console.setTextColor(Color.GREEN.id, bright: true);

print("written ${data.source} -> ${data.permalink()}");
print("written ${data.source} -> ${data.link()}");
});
}
}

class LiquidWriter extends Handle<Post> {
class LiquidWriter<T> extends Handle<Base> {
@override
Future<void> handle(Post data, HandleFunc<Post> next) async {
Future<void> handle(Base data, HandleFunc<Base> next) async {
try {
data.content = await Template(data.content).render();
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/generator/static.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:io';

import 'package:console/console.dart';
import 'package:gengen/generator/generator.dart';
import 'package:gengen/generator/pipeline.dart';
import 'package:gengen/pipeline/pipeline.dart';
import 'package:gengen/models.dart';

import 'handlers.dart';
Expand Down
Loading

0 comments on commit cfc7436

Please sign in to comment.