From f503e8be6c51d78864ca9629681255cf9c2a4e87 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Sat, 30 Jan 2016 16:02:49 +0000 Subject: [PATCH] Extract common code from scripts, add script to process post links, put links in list in top of post to allow links in excerpts --- _scripts/convert-links.php | 112 +++++++++++++++++++++------------- _scripts/generate-gallery.php | 31 ++-------- _scripts/utilities.php | 20 ++++++ 3 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 _scripts/utilities.php diff --git a/_scripts/convert-links.php b/_scripts/convert-links.php index 180428c..56c79e7 100644 --- a/_scripts/convert-links.php +++ b/_scripts/convert-links.php @@ -1,60 +1,86 @@ ([^<]+)~', $sContents, $aMatches, PREG_SET_ORDER)) { + foreach ($aMatches as $aMatch) { + $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); - echo " new file saved!\n"; -} + // 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); + } + } -function yamlDump($aData) { - return str_replace("'", null, Yaml::dump($aData, 4, 2)); + // 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"; } \ No newline at end of file diff --git a/_scripts/generate-gallery.php b/_scripts/generate-gallery.php index d0dc636..dbaeb03 100644 --- a/_scripts/generate-gallery.php +++ b/_scripts/generate-gallery.php @@ -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('index'); $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; @@ -320,13 +304,8 @@ $oConsole if ($oDate->diff($oEndDate)->d > 0) { $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) } ); diff --git a/_scripts/utilities.php b/_scripts/utilities.php new file mode 100644 index 0000000..874697c --- /dev/null +++ b/_scripts/utilities.php @@ -0,0 +1,20 @@ +