Skip to content

Instantly share code, notes, and snippets.

@brasofilo
Last active February 19, 2021 16:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brasofilo/e39b5d88b52a649eb3bb to your computer and use it in GitHub Desktop.
Save brasofilo/e39b5d88b52a649eb3bb to your computer and use it in GitHub Desktop.
Convert Codex XML to Apple Dictionary XML
<?php
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
echo '<d:dictionary xmlns="http://www.w3.org/1999/xhtml" xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng">' . "\r\n";
$codex = simplexml_load_file('codex.xml');
$count = 0;
foreach( $codex->page as $page )
{
$title = str_replace( 'Function Reference/', '', $page->title );
$id = str_replace( ' ', '_', $title );
# Avoid adding translated resources
if( false !== strpos( $title,':' ) )
continue;
printf('
<d:entry id="%1$s" d:title="%2$s">
<d:index d:value="%2$s"/>
<h1>%2$s</h1>
<p>%3$s</p>
</d:entry>',
$id,
$title,
htmlentities( $page->revision->text )
);
echo "\r\n";
}
echo '</d:dictionary>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment