diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2fd639a --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "gwhc/blog", + "description": "Simple blog management", + "type": "project", + "license": "ISC", + "authors": [ + { + "name": "Bill Ross", + "email": "wnross@gwhc.net" + } + ], + "require": { + "league/commonmark": "^2.7", + "symfony/yaml": "^6.0" + } +} diff --git a/index.php b/index.php index c435c3d..a2db71f 100644 --- a/index.php +++ b/index.php @@ -1,57 +1,62 @@ [ 'title' => 'articles' ], + 'html' => <<Placeholder for article listings + HTML, +]; } +$template = file_get_contents('template.inc'); + +$result = preg_replace_callback('/\[(\w+)\]/', function ($matches) use ($article) { + $key = $matches[1]; + return isset($article['meta'][$key]) ? $article['meta'][$key] : $matches[0]; // fallback to original if key not found +}, $template); + +$rendered= preg_replace('/\[contents?\]/', $article['html'], $result); + debug("PAGE: $page\n" ."TYPE: $type\n" + ."FILE: $file\n" ."PATH: {$_SERVER['PATH_INFO']}\n"); -# Load file, we do include rather than file_get_contents so PHP can be -# executed if needed -ob_start(); -include __DIR__ . '/' . $file; -$contents = ob_get_clean(); - -# Split off head matter -# -# this pattern uses the 'ms' modifier to treat multiline strings as a -# single string, and let '\s', '.' etc treat newlines as whitespace -# I match an _optional_ HTML ( '') comment if we want to wrap -# the headmatter in a comment. -# -# The headmatter is delimited by either '----' or '++++' as per -# Markdown/yaml standards. -# Technical note that '+' is a regex character so \+{3,} instead of ++++ -if (preg_match('/^()?(.*)/ms',$contents, $parts) === true) { - $var_dump($parts); -} - +echo $rendered; diff --git a/lang.md b/lang.md index 76b268d..9974bab 100644 --- a/lang.md +++ b/lang.md @@ -1,4 +1,4 @@ ----- +--- # Markdown Friendly Headmatter date: 2025-05-08 title: Why is translation important @@ -9,7 +9,7 @@ tags: - Multilingual websites - Customer trust - SEO optimization ----- +--- In today’s global marketplace, businesses of all sizes are reaching out to customers beyond their local borders. For small to medium-sized enterprises (SMEs), having a corporate website that speaks the language of potential customers is no longer just a nice-to-have; it’s essential. Here’s why translation is important for your corporate website. diff --git a/front.php b/lib/front.php similarity index 100% rename from front.php rename to lib/front.php diff --git a/lib/render_markdown.php b/lib/render_markdown.php new file mode 100644 index 0000000..c88166e --- /dev/null +++ b/lib/render_markdown.php @@ -0,0 +1,36 @@ +addExtension(new CommonMarkCoreExtension()); + $environment->addExtension(new GithubFlavoredMarkdownExtension()); + $environment->addExtension(new FrontMatterExtension()); + + $converter = new MarkdownConverter($environment); + + $markdownText = file_get_contents($file); + // Convert markdown and extract front matter + /** @var RenderedContentWithFrontMatter $result */ + $result = $converter->convert($markdownText); + + // Parse YAML front matter + if ($result instanceof RenderedContentWithFrontMatter) { + $frontMatter = $result->getFrontMatter(); + } + $html = $result->getContent(); + + // return result + return [ + 'meta' => $frontMatter, + 'html' => $html, + ]; +} diff --git a/lib/render_php.php b/lib/render_php.php new file mode 100644 index 0000000..e46b9ec --- /dev/null +++ b/lib/render_php.php @@ -0,0 +1,30 @@ +') comment if we want to wrap + # the headmatter in a comment. + # + # The headmatter is delimited by either '----' or '++++' as per + # Markdown/yaml standards. + # Technical note that '+' is a regex character so \+{3,} instead of ++++ + if (preg_match('/^()?\s*(.*)/ms',$html, $parts) === 1) { + $html = $parts[6]; + $meta = array_merge($meta,Yaml::parse($parts[3])); + } + + return [ + 'html' => $html, + 'meta' => $meta, + ]; +} diff --git a/template.inc b/template.inc new file mode 100644 index 0000000..9b0e66d --- /dev/null +++ b/template.inc @@ -0,0 +1,282 @@ + + + + + + + [title] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ + + + + + + + +
+ + + + + +
+
+

[title]

+
+
+
+ [content] +
+
+
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/template.inc.simple b/template.inc.simple new file mode 100644 index 0000000..07ad21c --- /dev/null +++ b/template.inc.simple @@ -0,0 +1,13 @@ + + + + [title] + + +

[title]

+

Description: [description] + +[contents] + + +