diff --git a/content-migration/hooks/migrate-hooks b/content-migration/hooks/migrate-hooks index 0a3106e1..6d6584ab 100755 --- a/content-migration/hooks/migrate-hooks +++ b/content-migration/hooks/migrate-hooks @@ -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; } } }