Skip to content

Commit

Permalink
hooks migrate - cleaning up markdown after conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
seancolsen committed Jan 29, 2017
1 parent 988a872 commit 1088458
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions content-migration/hooks/migrate-hooks
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,54 @@ $cache_dir = __DIR__ . '/wiki_cache';
$root_dir = dirname(dirname(__DIR__));
$hooks_dir = "$root_dir/docs/hooks";


function clean_markdown($markdown) {
// add domain name to all hyperlinks that point to the wiki
$pattern = '@\]\((/confluence[^)]*)\)@';
$replace = '](https://wiki.civicrm.org$1)';
$markdown = preg_replace($pattern,$replace,$markdown);

// set all headings of level 3 and above to level 2
$pattern = '@^(##)(#+) (.*)$@m';
$replace = '## $3';
$markdown = preg_replace($pattern,$replace,$markdown);

return $markdown;
}



# create hooks directory if needed
if ( !is_dir($hooks_dir) ) {
mkdir($hooks_dir);
}

chdir($hooks_dir);

foreach ($hooks_by_category as $category => $hooks) {
foreach ($hooks as $hook) {
$hook_name = $hook['name'];
$hook_file = "$hook_name.md";
$markdown_file = "$hook_name.md";
$html = "$cache_dir/$hook_name";
if ( file_exists($html) && !file_exists($hook_file) ) {
if ( file_exists($html) && !file_exists($markdown_file) ) {
echo "converting $hook_name ... ";
$conversion_status = 1;
system("webpage2md $html > $hook_file", $conversion_status);
// TODO fix links that point to wiki using only a slash
if( $conversion_status == 0 ) {
$conv_output_array = array();
$conv_status = 1;
exec("webpage2md $html", $conv_output_array, $conv_status);
if( $conv_status == 0 ) {
$conv_output = implode("\n",$conv_output_array);
$conv_output = clean_markdown($conv_output);
file_put_contents($markdown_file, $conv_output);
echo "done" . PHP_EOL;
}
else {
echo "ERROR CONVERTING $hook_name" . PHP_EOL;
echo "$conv_output";
}
}
else if ( !file_exists($html) ) {
echo "WARNING: $hook_name not yet fetched" . PHP_EOL;
}
else if ( file_exists($hook_file) ) {
echo "ignoring $hook_name (hook file already exists)" . PHP_EOL;
else if ( file_exists($markdown_file) ) {
echo "ignoring $hook_name (markdown file already exists)" . PHP_EOL;
}
}
}
Expand Down

0 comments on commit 1088458

Please sign in to comment.