Skip to content

Instantly share code, notes, and snippets.

@cafuego
Last active August 29, 2015 13:57
Show Gist options
  • Save cafuego/9794839 to your computer and use it in GitHub Desktop.
Save cafuego/9794839 to your computer and use it in GitHub Desktop.
Rewrite github generated HTML to Backdrop filtered HTML
#!/usr/bin/php
<?php
if ($_SERVER['argc'] == 2) {
$file = $_SERVER['argv'][1];
}
else {
$file = 'php://stdin';
}
$fp = fopen($file, 'r');
if (!$fp) {
die("Cannot read from ${file}.");
}
while (!feof($fp)) {
$line = fgets($fp, 4096);
$sane = rtrim($line);
$sane = strip_tags($sane, '<a><em><strong><cite><blockquote><code><ul><ol><li><h2><h3><h4>');
$sane = preg_replace('/([^<a]) id="([^"])+"/', '\\1', $sane);
$sane = preg_replace('/ class="([^"])+"/', '', $sane);
$sane = preg_replace('/ data-lang="([^"])+"/', '', $sane);
$sane = strtr($sane, array('&#39;' => "'", '&quot;' => '"', '&gt;' => '>', '&lt;' => '<', '&nbsp;' => ' '));
$sane = html_entity_decode($sane);
printf("%s\n", $sane);
}
fclose($fp);
@jenlampton
Copy link

++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment