Parse markdown into html before saving to DB

This commit is contained in:
Leonard Smith 2021-05-19 06:41:53 -05:00
parent 8f477f1e32
commit ee92322c80
5 changed files with 161 additions and 6 deletions

View File

@ -0,0 +1,78 @@
<?php
namespace App\Handlers;
/**
* CommentaryImportHandler.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 5/6/21
* Time: 2:04 PM
*/
class CommentaryImportHandler
{
const REPO_FOLDER = 'en_bc';
public function run(): void
{
// Iterate over each folder in the repo
$handle = opendir($this->getFolder());
while (($dir = readdir($handle)) !== false) {
$bookNumber = intval(substr($dir, 0, 2));
if ($bookNumber > 40 && $bookNumber < 68) {
// Import all of the chapter data
if (is_dir($this->getFolder() . $dir)) {
$this->scanCommentaryChapterDirectory($this->getFolder() . $dir);
}
} elseif ($dir === 'articles') {
$this->scanCommentaryArticles($this->getFolder() . $dir);
};
// Silently ignoring files at this level
}
closedir($handle);
}
protected function scanCommentaryArticles(string $dir): void
{
$handle = opendir($dir);
while (($item = readdir($handle)) !== false) {
$this->importArticle(file_get_contents($dir . '/' . $item));
}
}
protected function importArticle(string $content) : void
{
}
protected function scanCommentaryChapterDirectory(string $dir): void
{
$handle = opendir($dir);
while (($item = readdir($handle)) !== false) {
$contents = file_get_contents($dir . '/' . $item);
if (is_numeric(subst($item, 0, 2))) {
$this->importCommentaryChapterContent($contents);
} elseif (substr($item, 0, 5) === 'intro') {
$this->importCommentaryBookIntro($contents);
}
}
}
protected function importCommentaryChapterContent(string $content)
{
}
protected function importCommentaryBookIntro(string $contents)
{
}
protected function getFolder() : string
{
return storage_path() . self::REPO_FOLDER;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Handlers;
use App\LexicalEntry;
use GrahamCampbell\Markdown\Facades\Markdown;
/**
* LexiconEntryImportHandler.php
*
@ -52,7 +53,7 @@ class LexiconEntryImportHandler
$entry = LexicalEntry::create([
'id' => $stongsNumber,
'lemma' => $lemma,
'commentary' => $commentary,
'commentary' => Markdown::convertToHtml($commentary),
]);
return $entry;

View File

@ -22,17 +22,17 @@ trait LexiconEntryTrait
$commentary = '';
$n = 0;
foreach(preg_split('~[\r\n]+~', $entry) as $line){
if(empty($line) or ctype_space($line)) continue; // skip only spaces
foreach(preg_split('~[\r\n?]|[\r?\n]+~', $entry) as $line){
// if(empty($line) or ctype_space($line)) continue; // skip only spaces
if ($n === 0) {
$lexeme = ltrim($line, '#');
$lexeme = ltrim($line, '# ');
} else {
$commentary .= $line . "\n";
}
$n++;
}
return [$lexeme, $commentary];
return [$lexeme, trim($commentary)];
}
}

View File

@ -12,6 +12,7 @@
"cloudcreativity/laravel-json-api": "^2.2",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^2.0",
"graham-campbell/markdown": "^13.1",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.24",
"laravel/tinker": "^2.0"

77
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "a4517033c786b889ade7cf9ae70688fc",
"content-hash": "30f9b75c0f341616d5dd182213450b11",
"packages": [
{
"name": "asm89/stack-cors",
@ -635,6 +635,81 @@
],
"time": "2020-09-07T11:48:52+00:00"
},
{
"name": "graham-campbell/markdown",
"version": "v13.1.1",
"source": {
"type": "git",
"url": "https://github.com/GrahamCampbell/Laravel-Markdown.git",
"reference": "d25b873e5c5870edc4de7d980808f1a8e092a9c7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/GrahamCampbell/Laravel-Markdown/zipball/d25b873e5c5870edc4de7d980808f1a8e092a9c7",
"reference": "d25b873e5c5870edc4de7d980808f1a8e092a9c7",
"shasum": ""
},
"require": {
"illuminate/contracts": "^6.0 || ^7.0 || ^8.0",
"illuminate/filesystem": "^6.0 || ^7.0 || ^8.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0",
"illuminate/view": "^6.0 || ^7.0 || ^8.0",
"league/commonmark": "^1.5",
"php": "^7.2.5 || ^8.0"
},
"require-dev": {
"graham-campbell/analyzer": "^3.0",
"graham-campbell/testbench": "^5.4",
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^8.5.8 || ^9.3.7"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"GrahamCampbell\\Markdown\\MarkdownServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"GrahamCampbell\\Markdown\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "graham@alt-three.com"
}
],
"description": "Markdown Is A CommonMark Wrapper For Laravel",
"keywords": [
"Graham Campbell",
"GrahamCampbell",
"Laravel Markdown",
"Laravel-Markdown",
"common mark",
"commonmark",
"framework",
"laravel",
"markdown"
],
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/graham-campbell/markdown",
"type": "tidelift"
}
],
"time": "2020-08-22T14:18:21+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.5.5",