Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Sublime Text snippet documentation table generator for MediaWiki.
- *
- * @author Marijke Luttekes
- */
- /*
- |------------------------------------------------------------------------------
- | Environment settings
- |------------------------------------------------------------------------------
- */
- $dirpath = 'Snippets';
- /*
- |------------------------------------------------------------------------------
- | Extract snippets from directory
- |------------------------------------------------------------------------------
- */
- // Make sure the directory exists
- if (!is_dir($dirpath)) {
- exit('[EXIT] The directory "'.$dirpath.'" does not exist.');
- }
- // Make sure we can open the directory
- if (!$dir = opendir($dirpath)) {
- exit('[EXIT] Could not open directory "'.$dirpath.'".');
- }
- // Time to extract those snippets
- $snippets = array();
- while ($filepath = readdir($dir)) {
- // Snippets should always have the extensions 'sublime-snippet'
- if (pathinfo($filepath, PATHINFO_EXTENSION) != 'sublime-snippet') {
- continue;
- }
- // Snippets are written in XML, so lets parse this stuff
- $snippet = new DOMDocument();
- $snippet->load($dirpath.'/'.$filepath);
- foreach ($snippet->getElementsByTagName('snippet') as $value) {
- $name = pathinfo($filepath, PATHINFO_FILENAME);
- // Use the file's name as a unique identifier
- $snippets[$name] = array(
- 'file' => $filepath
- );
- // Put the snippet's child nodes into the array
- foreach($value->childNodes as $child) {
- // There's no need to save the actual content of the snippet, we only need its properties
- if ($child->nodeName == 'content' || empty(trim($child->textContent))) {
- continue;
- }
- // Accepted node names are usually: {tabTrigger, scope, description}
- $snippets[$name][$child->nodeName] = $child->textContent;
- }
- }
- }
- /*
- |------------------------------------------------------------------------------
- | Convert snippet results to MediaWiki table markup
- |------------------------------------------------------------------------------
- */
- // No snippets, no glory
- if (empty($snippets)) {
- exit('[EXIT] No snippets were found in directory "'.$dirpath.'".');
- }
- // Generate the MediaWiki table
- $markup = '{| {{STDT|sortable}}'.PHP_EOL;
- $markup .= '! scope="col" | Name'.PHP_EOL;
- $markup .= '! scope="col" | Tab trigger'.PHP_EOL;
- $markup .= '! scope="col" | Type(s)'.PHP_EOL;
- $markup .= '! scope="col" | Description'.PHP_EOL;
- $markup .= '! scope="col" | Filename'.PHP_EOL;
- $markup .= '! scope="col" | Notes'.PHP_EOL;
- foreach ($snippets as $name => $data) {
- // Extract all data from $snippets
- $name = strstr($name, '-') ? substr(strstr($name, '-'), 1) : $name;
- $tab = isset($data['tabTrigger']) && !empty($data['tabTrigger']) ? $data['tabTrigger'] : '';
- $scope = isset($data['scope']) && !empty($data['scope']) ? $data['scope'] : '';
- $desc = isset($data['description']) && !empty($data['description']) ? $data['description'] : '';
- // Remove "text." and "source." from scope(s)
- $types = explode(',', $scope);
- foreach ($types as &$value) {
- $value = substr(strrchr($value, '.'), 1);
- }
- sort($types);
- $types = implode(', ', $types);
- // Generate the table row
- $markup .= '|-'.PHP_EOL;
- $markup .= '| '.$name.PHP_EOL;
- $markup .= '| '.$tab.PHP_EOL;
- $markup .= '| '.$types.PHP_EOL;
- $markup .= '| '.$desc.PHP_EOL;
- $markup .= '| '.$data['file'].PHP_EOL;
- $markup .= '| -'.PHP_EOL;
- }
- $markup .= '|}';
- // Print the markup, the markup can be directly pasted into a MediaWiki page
- echo '<pre><code>';
- echo $markup;
- echo '</code></pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement