<?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright https://yawik.org/COPYRIGHT.php
 * @license   MIT
 */

$docType = (string) $this->doctype();


$headTitle = (string) $this->headTitle()->setAutoEscape(false);

$viewPort = (string) $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0');

$basePath = $this->basePath();
$headLink = (string) $this->headLink();
$headScript = (string) $this->headScript();
$headStyle = (string) $this->headStyle();

$addHead = '';
$addHead .= empty($headLink)?'':($headLink . PHP_EOL);
$addHead .= empty($headScript)?'':($headScript . PHP_EOL);
$addHead .= empty($headStyle)?'':($headStyle . PHP_EOL);
$addHead .= '<script type="text/javascript">
    function yawikPrintJob() {
        window.print();
    }
</script>';

$content = $this->content;
if (!empty($addHead)) {

    if (preg_match("/^(.*)(<\/head>[^\w]*<body>.*)$/ms", $this->content, $matches)) {
        $head = $matches[1];
        $body = $matches[2];
    } else {
        $head = '';
        $body = $this->content;
    }
    //$content = $head . PHP_EOL . $headLink . PHP_EOL . $headScript . PHP_EOL .  $headStyle . PHP_EOL . $body;
    $content = $head . PHP_EOL . $addHead . $body;
} else {

}

echo $content;

?>