Skip to content

Commit

Permalink
Add initial tools
Browse files Browse the repository at this point in the history
rmccue committed Jul 18, 2017
0 parents commit 2fe64e7
Showing 2 changed files with 106 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bin/generate-manifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env php
<?php

$root = dirname( __DIR__ );
$repo = 'rmccue/docs-testing';

$manifest = array();
$paths = array(
$root . '/*.md',
$root . '/*/*.md',
$root . '/*/*/*.md',
);
$excludes = array(
$root . '/README.md',
);
foreach ( $paths as $path ) {
foreach ( glob( $path ) as $file ) {
if ( in_array( $file, $excludes ) ) {
continue;
}

$slug = basename( $file, '.md' );
$key = str_replace( array( $root . '/', '.md' ), '', $file );
$parent = null;
if ( stripos( $key, '/' ) ) {
$bits = explode( '/', $key );
array_pop( $bits );
$parent = implode( '/', $bits );
}
$manifest[ $key ] = array(
'slug' => $slug,
'parent' => $parent,
'markdown_source' => sprintf( 'https://github.com/%s/blob/master/%s.md', $repo, $key ),
);
}
}

file_put_contents( $root . '/bin/manifest.json', json_encode( (object) $manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );

$count = count( $manifest );
echo "Generated manifest.json of {$count} pages\n";
65 changes: 65 additions & 0 deletions bin/parse-wxr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

$root = dirname( __DIR__ );
$reader = simplexml_load_file( __DIR__ . '/import.xml' );

// HTML is valid Markdown, but we should replace for comfort's sake.
$markdown_map = [
'<h2>' => '## ',
'</h2>' => '',
'<h3>' => '### ',
'</h3>' => '',
'[php]' => '```php',
'&lt;?php' => '<' . '?php',
'[/php]' => '```',
'<code>' => '`',
'</code>' => '`',
'<em>' => '*',
'</em>' => '*',
'<strong>' => '**',
'</strong>' => '**',
'' => '"',
'' => '"',
'' => "'",
];

foreach ( $reader->channel->item as $item ) {
$title = (string) $item->title;
$url = (string) $item->link;
$content = (string) $item->children( 'http://purl.org/rss/1.0/modules/content/' )->encoded;

$wxr_item = $item->children( 'http://wordpress.org/export/1.2/' );

$status = (string) $wxr_item->status;
if ( $status !== 'publish' ) {
printf( "Skipping draft %s\n", $title );
continue;
}

$slug = str_replace( 'https://developer.wordpress.org/rest-api/', '', $url );
if ( empty( $slug ) ) {
$slug = 'index';
}

$slug = rtrim( $slug, '/' ) . '.md';

$path = $root . '/' . $slug;
if ( ! file_exists( dirname( $path ) ) ) {
mkdir( dirname( $path ), 0755, true );
}
printf( "Creating %s: %s\n", $title, $path );

$markdowned = str_replace(
array_keys( $markdown_map ),
array_values( $markdown_map ),
$content
);
$markdowned = preg_replace(
'#<a href="([^"]+)">([^<]+)</a>#i',
'[\2](\1)',
$markdowned
);
$data = sprintf( "# %s\n\n%s\n", $title, $markdowned );
// echo str_replace( "\n", "\n\t", $data ) . "\n\n";
file_put_contents( $path, $data );
}

0 comments on commit 2fe64e7

Please sign in to comment.