Extract common code from scripts, add script to process post links, put links in list in top of post to allow links in excerpts

This commit is contained in:
2016-01-30 16:02:49 +00:00
parent 098debb80d
commit f503e8be6c
3 changed files with 94 additions and 69 deletions

View File

@@ -1,60 +1,86 @@
<?php
require __DIR__ . '/../_php/autoload.php';
require __DIR__ . '/utilities.php';
use Symfony\Component\Yaml\Yaml;
$sNewDir = 'blog/_posts.new';
$sOldDir = 'blog/_posts.old';
$sNewDir = 'blog/_posts';
if (!is_dir($sNewDir)) {
mkdir($sNewDir);
}
$aPosts = [];
foreach (glob('blog/_posts/*.md') as $sFile) {
foreach (glob(sprintf('%s/*.md', $sOldDir)) as $sFile) {
$sBasename = basename($sFile, '.md');
$sContents = file_get_contents($sFile);
$aContents = explode("\n", $sContents);
$aParts = explode('--', implode("\n", array_slice($aContents, 1)));
$aYaml = Yaml::parse(trim(str_replace(" - \n", null, current($aParts))));
if (isset($aYaml['id'])) {
$aPosts[$aYaml['id']] = $sBasename;
}
}
foreach (glob('blog/_posts/*.md') as $sFile) {
$sBasename = basename($sFile);
printf("%s...\n", $sBasename);
parseFile($sFile, $aYaml, $sContents);
$sContents = file_get_contents($sFile);
$aContents = explode("\n", $sContents);
$aParts = explode('--', implode("\n", array_slice($aContents, 1)));
$aYaml = Yaml::parse(trim(str_replace(" - \n", null, current($aParts))));
$sContents = end($aParts);
if (!preg_match_all('~/\?p=([\d]+)~', $sContents, $aMatches, PREG_SET_ORDER)) {
echo " no local urls found\n";
continue;
if (isset($aYaml['end_date'])) {
$aYaml['end_date'] = date('Y-m-d', $aYaml['end_date']);
}
// Replace html links
if (preg_match_all('~<a.*?href="([^"]+)".*?>([^<]+)</a>~', $sContents, $aMatches, PREG_SET_ORDER)) {
foreach ($aMatches as $aMatch) {
$iId = $aMatch[1];
if (isset($aPosts[$iId])) {
$sPost = $aPosts[$iId];
printf(" substituting id (%d) with: %s\n", $iId, $sPost);
$sContents = str_replace($aMatch[0], sprintf('{%% post_link %s %%}', $sPost), $sContents);
} else {
printf(" unknown id: %d\n", $iId);
$sTitle = preg_match('~title="([^"]+)"~', $aMatch[0], $aTitleMatch) ? sprintf(' "%s"', $aTitleMatch[1]) : null;
printf(" substituting: %s\n", $aMatch[1]);
$sContents = str_replace($aMatch[0], sprintf('[%s](%s%s)', $aMatch[2], $aMatch[1], $sTitle), $sContents);
}
}
$sNewFile = sprintf('%s/%s', $sNewDir, $sBasename);
$sNewContents = '---' . "\n" . yamlDump($aYaml) . '---' . "\n" . trim($sContents);
file_put_contents($sNewFile, $sNewContents);
// Find markdown foot links
$aLinks = [];
if (preg_match_all('~[\s]*\[([\d]+)\]:([^\n]+)~', $sContents, $aMatches, PREG_SET_ORDER)) {
foreach ($aMatches as $aMatch) {
if (isset($aLinks[$aMatch[1]])) {
printf("Duplicate id: %d\n", $aMatch[1]);
exit;
}
$aLinks[$aMatch[1]] = $aMatch[2];
$sContents = str_replace($aMatch[0], null, $sContents);
}
}
// Find references to foot links
$aTable = [];
$sNewContents = $sContents;
if (preg_match_all('~\[([^\]]+)\]\[([^\]]+)\]~', $sContents, $aMatches, PREG_SET_ORDER)) {
foreach ($aMatches as $aMatch) {
if (!isset($aLinks[$aMatch[2]])) {
printf("Undefined id: %d\n", $aMatch[2]);
exit;
}
$sReference = sprintf('[%s][%%d]', $aMatch[1]);
$aTable[strpos($sContents, $aMatch[0])] = array($sReference, $aLinks[$aMatch[2]]);
$sNewContents = str_replace($aMatch[0], $sReference, $sNewContents);
}
}
$sContents = $sNewContents;
// Find all markdown links
$sNewContents = $sContents;
if (preg_match_all('~\[([^\]]+)\]\(([^\)]+)(?:[\s]*"([^"]+)")?\)~', $sContents, $aMatches, PREG_SET_ORDER)) {
foreach ($aMatches as $aMatch) {
$sLink = sprintf('%s%s', $aMatch[2], isset($aMatch[3]) ? sprintf(' "%s"', $aMatch[3]) : null);
$sReference = sprintf('[%s][%%d]', $aMatch[1]);
$aTable[strpos($sContents, $aMatch[0])] = array($sReference, $sLink);
$sNewContents = str_replace($aMatch[0], $sReference, $sNewContents);
}
}
$sContents = $sNewContents;
// Place sorted links
if (count($aTable) > 0) {
$aLinks = [];
ksort($aTable);
foreach (array_values($aTable) as $i => $aLink) {
$sContents = str_replace($aLink[0], sprintf($aLink[0], $i + 1), $sContents);
$aLinks[] = sprintf('[%d]: %s', $i + 1, trim($aLink[1]));
}
$sContents = sprintf("%s\n\n%s", implode("\n", $aLinks), trim($sContents));
$iLinks = count($aLinks);
printf(" Placing %d link%s!\n", $iLinks, $iLinks > 1 ? 's' : null);
}
$sNewFile = sprintf('%s/%s.md', $sNewDir, $sBasename);
writeFile($sNewFile, $aYaml, $sContents);
echo " new file saved!\n";
}
function yamlDump($aData) {
return str_replace("'", null, Yaml::dump($aData, 4, 2));
}

View File

@@ -33,20 +33,15 @@ $oConsole
$sGallery = $oInput->getArgument('name');
$sDir = realpath(rtrim($oInput->getArgument('dir'), '/\\'));
$sAssetPath = $oInput->getArgument('assetdir') . '/' . $sGallery;
$sRenderPath = $oInput->getArgument('mdowndir') . '/' . $sGallery;
$sLayout = $oInput->getOption('layout');
$sExports = $oInput->getOption('export');
$bSkipResize = $oInput->getOption('skip-resize');
$oImagine = new Imagine\Gd\Imagine();
// Initialize directories
// Initialize directory
if (!is_dir($sAssetPath)) {
mkdir($sAssetPath, 0700, true);
}
if (!is_dir($sRenderPath)) {
mkdir($sRenderPath, 0700, true);
}
// Check directory and presence of YAML file
if (!is_dir($sDir)) {
@@ -87,11 +82,6 @@ $oConsole
$aPhoto['id'] .= '-' . preg_replace('/(-| )+/', '-', preg_replace('/[^a-z0-9 ]/i', '-', preg_replace('/\'/', '', strtolower(preg_replace('/\p{Mn}/u', '', Normalizer::normalize($aPhoto['title'], Normalizer::FORM_KD))))));
}
// Check if photo is highlighted
if (empty($sHighlight) && isset($aMeta[$sFile]['highlight'])) {
$sHighlight = $aPhoto['id'];
}
// Parse selected EXIF data
$aPhoto['exif'] = exif_read_data($aPhoto['path']);
if (isset($aPhoto['exif']['GPSLongitude'])) {
@@ -286,21 +276,15 @@ $oConsole
: (($iSurfaceA > $iSurfaceB) ? -1 : 1);
}
);
// Write photo Markdown file
file_put_contents(
$sRenderPath . '/' . $aPhoto['id'] . '.md',
'---' . "\n" . yamlDump($aMatter) . '---' . "\n" . (empty($aPhoto['comment']) ? '' : $aPhoto['comment'] . "\n")
);
// yamlDump($aMatter)
$oOutput->writeln(' done');
}
// Write gallery index
// Write datafile
$oOutput->write('<comment>index</comment>');
$aMatter = [
'layout' => 'gallery-list',
'title' => empty($aGallery['title']) ? '' : $aGallery['title'],
'highlight_photo' => empty($sHighlight) ? $aPhotos[0]['id'] : $sHighlight,
'date' => $oDate->format('Y-m-d')
];
$bSlideshow = isset($aGallery['slideshow']) ? $aGallery['slideshow'] : true;
@@ -321,12 +305,7 @@ $oConsole
$aMatter['end_date'] = $oEndDate->format('Y-m-d');
}
$sContents = empty($aGallery['description']) ? '' : $aGallery['description'];
file_put_contents(
$sRenderPath . '/index.html',
'---' . "\n" . yamlDump($aMatter) . '---' . "\n" . $sContents
);
$oOutput->writeln(' done');
// yamlDump($aMatter)
}
);

20
_scripts/utilities.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
require __DIR__ . '/../_php/autoload.php';
use Symfony\Component\Yaml\Yaml;
function parseFile($sFile, &$aYaml, &$sContents = null) {
$sContents = file_get_contents($sFile);
$aContents = explode("\n", $sContents);
$aParts = explode('---', implode("\n", array_slice($aContents, 1)));
$aYaml = Yaml::parse(trim(str_replace(" - \n", null, current($aParts))));
$sContents = trim(implode('---', array_slice($aParts, 1)));
}
function writeFile($sFile, $aYaml, $sContents = null) {
file_put_contents($sFile, '---' . "\n" . yamlDump($aYaml) . '---' . "\n" . trim($sContents));
}
function yamlDump($aData) {
return str_replace("'", null, Yaml::dump($aData, 4, 2));
}