2021-10-05 19:13:12 +02:00
#!/usr/bin/php
2020-08-02 14:15:06 +02:00
< ? php
2021-10-05 19:13:12 +02:00
if ( php_sapi_name () !== " cli " )
exit ( " Must be run from CLI " );
2021-10-06 18:39:25 +02:00
// Initialization
2021-10-05 19:13:12 +02:00
define ( " FIND " , " /usr/bin/find " );
define ( " GZIP " , " /usr/bin/gzip " );
define ( " ROOT " , dirname ( $_SERVER [ 'SCRIPT_FILENAME' ]));
define ( " SITE " , $argv [ 1 ]);
if ( isset ( $argv [ 2 ]))
2021-09-15 23:15:48 +02:00
define ( " DESTINATION " , $argv [ 2 ]);
2021-10-05 19:13:12 +02:00
else
define ( " DESTINATION " , " local " );
2021-10-06 18:39:25 +02:00
if ( file_exists ( SITE . " /config.ini " ))
$config = parse_ini_file ( SITE . " /config.ini " );
if ( ! isset ( $config [ 'trueBlack' ]))
$config [ 'trueBlack' ] = false ;
if ( ! isset ( $config [ 'siteTitle' ]))
$config [ 'siteTitle' ] = " Site " ;
if ( ! isset ( $config [ 'css' ]))
$config [ 'css' ] = true ;
if ( ! isset ( $config [ 'header' ]))
$config [ 'header' ] = true ;
if ( ! isset ( $config [ 'centerIndex' ]))
$config [ 'centerIndex' ] = false ;
2021-10-05 19:13:12 +02:00
// Less > CSS
2021-10-06 18:39:25 +02:00
2021-10-05 19:13:12 +02:00
require ROOT . " /bibli/less.php/lib/Less/Autoloader.php " ;
Less_Autoloader :: register ();
2021-10-06 18:39:25 +02:00
$colorScheme = array (
" darkColor " => " #2a2a2a " ,
" darkerColor " => " #222222 " ,
" lightColor " => " white " ,
" lightlessColor " => " #eeeeee " ,
" mainColor " => " red " ,
);
if ( $config [ 'trueBlack' ]) {
$colorScheme [ 'darkColor' ] = " #000000 " ;
$colorScheme [ 'darkerColor' ] = " #000000 " ;
}
2021-10-05 19:13:12 +02:00
$options = array ( 'cache_dir' => SITE . '/css' , 'compress' => true );
if ( file_exists ( SITE . " /style.less " ))
$lessFiles = array ( ROOT . '/style.less' => '' , SITE . '/style.less' => '' );
else
$lessFiles = array ( ROOT . '/style.less' => '' );
2020-08-02 14:15:06 +02:00
2021-10-06 18:39:25 +02:00
define ( " CSS_FILENAME " , Less_Cache :: Get ( $lessFiles , $options , $colorScheme ));
2021-09-21 19:19:05 +02:00
2021-10-05 19:13:12 +02:00
exec ( FIND . " " . SITE . " -name '*.gmi' -o -name '*.md' " , $pages );
2021-04-05 18:32:22 +02:00
2021-10-06 18:39:25 +02:00
// URLs don't end with .html on the Antopie website
function formerUrlLocale ( $page ) {
if ( DESTINATION === " dns " OR DESTINATION === " onion " ) {
echo $page ;
} else {
echo $page . " .html " ;
}
}
// Determine whether links need to use Onion or DNS
function clearnetOrOnion ( $clearnetUrl , $onionUrl ) {
if ( DESTINATION === " onion " ) {
return $onionUrl ;
} else {
return $clearnetUrl ;
}
}
2021-04-05 18:32:22 +02:00
2021-10-05 19:13:12 +02:00
foreach ( $pages as $page ) {
2021-04-05 18:32:22 +02:00
2021-10-05 19:13:12 +02:00
$pathParts = pathinfo ( $page );
2020-08-02 14:15:06 +02:00
2021-10-05 19:13:12 +02:00
// Convert Gemtext to Markdown
if ( $pathParts [ 'extension' ] === " gmi " ) {
2021-09-21 19:19:05 +02:00
$gmilines = explode ( " \n " , file_get_contents ( $page ));
if ( substr ( $gmilines [ 0 ], 0 , 2 ) === " # " )
$title = substr ( $gmilines [ 0 ], 2 );
else
$title = NULL ;
2021-09-18 19:09:11 +02:00
foreach ( $gmilines as $key => $line ) {
if ( substr ( $line , 0 , 2 ) === " => " ) {
2021-09-21 19:19:05 +02:00
preg_match ( " /=> +(.[^ ]+)/ " , $line , $lnUrl );
preg_match ( " /=> +.[^ ]+ +(.+)/ " , $line , $lnTitle );
2021-10-05 19:13:12 +02:00
// Escape Markdown special characters
2021-09-18 19:09:11 +02:00
$mdSpecial = array ( " [ " , " ] " , " ( " , " ) " );
$htmlEntities = array ( " [ " , " ] " , " ( " , " ) " );
2021-09-21 19:19:05 +02:00
$lnUrl [ 1 ] = str_replace ( $mdSpecial , $htmlEntities , $lnUrl [ 1 ]);
2021-10-05 19:13:12 +02:00
$urlPathParts = pathinfo ( parse_url ( $lnUrl [ 1 ], PHP_URL_PATH ));
// .gmi > .md for local links
if ( ! str_contains ( $lnUrl [ 1 ], " : " ) AND $urlPathParts [ 'extension' ] === " gmi " ) // If it's a local link
$lnUrl [ 1 ] = $urlPathParts [ 'dirname' ] . " / " . $urlPathParts [ 'filename' ] . " .md " ;
2021-09-21 19:19:05 +02:00
if ( isset ( $lnTitle [ 1 ])) {
$lnTitle [ 1 ] = str_replace ( $mdSpecial , $htmlEntities , $lnTitle [ 1 ]);
$gmilines [ $key ] = " [ " . $lnTitle [ 1 ] . " ]( " . $lnUrl [ 1 ] . " ) " ;
2021-09-18 19:09:11 +02:00
} else {
2021-09-21 19:19:05 +02:00
$gmilines [ $key ] = " [ " . $lnUrl [ 1 ] . " ]( " . $lnUrl [ 1 ] . " ) " ;
2021-09-18 19:09:11 +02:00
}
}
}
$code = " " ;
foreach ( $gmilines as $line ) {
$code = $code . " \n " . $line ;
}
2021-10-05 19:13:12 +02:00
file_put_contents ( $pathParts [ 'dirname' ] . " / " . $pathParts [ 'filename' ] . " .md " , $code );
}
// Execute PHP code
ob_start ();
eval ( " ?> " . file_get_contents ( $pathParts [ 'dirname' ] . " / " . $pathParts [ 'filename' ] . " .md " ));
$pageContent = ob_get_contents ();
ob_end_clean ();
// Compile Markdown to HTML with Parsedown
2021-10-06 18:39:25 +02:00
require_once ROOT . " /bibli/parsedown/Parsedown.php " ;
require_once ROOT . " /bibli/parsedown-extra/ParsedownExtra.php " ;
2021-10-05 19:13:12 +02:00
$Parsedown = new ParsedownExtra ;
$Parsedown = $Parsedown -> setUrlsLinked ( false );
$Parsedown = $Parsedown -> setMarkupEscaped ( false );
$Parsedown = $Parsedown -> setBreaksEnabled ( true );
$pageContent = $Parsedown -> text ( $pageContent );
// .md > .html for local links
$pageContent = preg_replace ( '#<a href="(?!.*:)(.*)\.md">#' , '<a href="$1.html">' , $pageContent );
2021-09-18 19:09:11 +02:00
2020-08-02 14:15:06 +02:00
2021-10-05 19:13:12 +02:00
// Add header and footer to HTML
ob_start ();
require " inc/debut.php " ;
2021-10-06 18:39:25 +02:00
if ( $config [ 'centerIndex' ] AND $pathParts [ 'filename' ] === " index " ) {
2021-10-05 19:13:12 +02:00
echo " <div class='centre'> " ;
} else {
echo " <article> " ;
}
echo $pageContent ;
2021-10-06 18:39:25 +02:00
if ( $config [ 'centerIndex' ] AND $pathParts [ 'filename' ] === " index " ) {
2021-10-05 19:13:12 +02:00
echo " </div> " ;
} else {
echo " </article> " ;
2020-08-02 14:15:06 +02:00
}
2021-10-05 19:13:12 +02:00
require " inc/footer.php " ;
file_put_contents ( $pathParts [ 'dirname' ] . " / " . $pathParts [ 'filename' ] . " .html " , ob_get_contents ());
ob_end_clean ();
2020-08-02 14:15:06 +02:00
2021-10-05 19:13:12 +02:00
// Gzip compression
exec ( GZIP . " --keep --fast --force " . $pathParts [ 'dirname' ] . " / " . $pathParts [ 'filename' ] . " .html " );
2020-08-02 14:15:06 +02:00
}
2021-10-05 19:13:12 +02:00
exec ( GZIP . " --keep --fast --force " . SITE . " /css/ " . CSS_FILENAME );