Advertisement
MHLut

Sublime Text snippet doc table generator for MediaWiki

Jul 30th, 2014
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Sublime Text snippet documentation table generator for MediaWiki.
  4.  *
  5.  * @author  Marijke Luttekes
  6.  */
  7.  
  8. /*
  9. |------------------------------------------------------------------------------
  10. | Environment settings
  11. |------------------------------------------------------------------------------
  12. */
  13.  
  14. $dirpath = 'Snippets';
  15.  
  16. /*
  17. |------------------------------------------------------------------------------
  18. | Extract snippets from directory
  19. |------------------------------------------------------------------------------
  20. */
  21.  
  22. // Make sure the directory exists
  23. if (!is_dir($dirpath)) {
  24.     exit('[EXIT] The directory "'.$dirpath.'" does not exist.');
  25. }
  26.  
  27. // Make sure we can open the directory
  28. if (!$dir = opendir($dirpath)) {
  29.     exit('[EXIT] Could not open directory "'.$dirpath.'".');
  30. }
  31.  
  32. // Time to extract those snippets
  33. $snippets = array();
  34. while ($filepath = readdir($dir)) {
  35.     // Snippets should always have the extensions 'sublime-snippet'
  36.     if (pathinfo($filepath, PATHINFO_EXTENSION) != 'sublime-snippet') {
  37.         continue;
  38.     }
  39.  
  40.     // Snippets are written in XML, so lets parse this stuff
  41.     $snippet = new DOMDocument();
  42.     $snippet->load($dirpath.'/'.$filepath);
  43.  
  44.     foreach ($snippet->getElementsByTagName('snippet') as $value) {
  45.         $name = pathinfo($filepath, PATHINFO_FILENAME);
  46.        
  47.         // Use the file's name as a unique identifier
  48.         $snippets[$name] = array(
  49.             'file' => $filepath
  50.         );
  51.  
  52.         // Put the snippet's child nodes into the array
  53.         foreach($value->childNodes as $child) {
  54.             // There's no need to save the actual content of the snippet, we only need its properties
  55.             if ($child->nodeName == 'content' || empty(trim($child->textContent))) {
  56.                 continue;
  57.             }
  58.  
  59.             // Accepted node names are usually: {tabTrigger, scope, description}
  60.             $snippets[$name][$child->nodeName] = $child->textContent;
  61.         }
  62.     }
  63. }
  64.  
  65. /*
  66. |------------------------------------------------------------------------------
  67. | Convert snippet results to MediaWiki table markup
  68. |------------------------------------------------------------------------------
  69. */
  70.  
  71. // No snippets, no glory
  72. if (empty($snippets)) {
  73.     exit('[EXIT] No snippets were found in directory "'.$dirpath.'".');
  74. }
  75.  
  76. // Generate the MediaWiki table
  77. $markup = '{| {{STDT|sortable}}'.PHP_EOL;
  78. $markup .= '! scope="col" | Name'.PHP_EOL;
  79. $markup .= '! scope="col" | Tab trigger'.PHP_EOL;
  80. $markup .= '! scope="col" | Type(s)'.PHP_EOL;
  81. $markup .= '! scope="col" | Description'.PHP_EOL;
  82. $markup .= '! scope="col" | Filename'.PHP_EOL;
  83. $markup .= '! scope="col" | Notes'.PHP_EOL;
  84. foreach ($snippets as $name => $data) {
  85.     // Extract all data from $snippets
  86.     $name = strstr($name, '-') ? substr(strstr($name, '-'), 1) : $name;
  87.     $tab = isset($data['tabTrigger']) && !empty($data['tabTrigger']) ? $data['tabTrigger'] : '';
  88.     $scope = isset($data['scope']) && !empty($data['scope']) ? $data['scope'] : '';
  89.     $desc = isset($data['description']) && !empty($data['description']) ? $data['description'] : '';
  90.  
  91.     // Remove "text." and "source." from scope(s)
  92.     $types = explode(',', $scope);
  93.     foreach ($types as &$value) {
  94.         $value = substr(strrchr($value, '.'), 1);
  95.     }
  96.     sort($types);
  97.     $types = implode(', ', $types);
  98.  
  99.     // Generate the table row
  100.     $markup .= '|-'.PHP_EOL;
  101.     $markup .= '| '.$name.PHP_EOL;
  102.     $markup .= '| '.$tab.PHP_EOL;
  103.     $markup .= '| '.$types.PHP_EOL;
  104.     $markup .= '| '.$desc.PHP_EOL;
  105.     $markup .= '| '.$data['file'].PHP_EOL;
  106.     $markup .= '| -'.PHP_EOL;
  107. }
  108. $markup .= '|}';
  109.  
  110. // Print the markup, the markup can be directly pasted into a MediaWiki page
  111. echo '<pre><code>';
  112. echo $markup;
  113. echo '</code></pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement