index.phps

<?php

$simplenote_url = "https://app.simplenote.com/publish/Hy0tKx";
#$hackpad_url = "https://scottywz.hackpad.com/n3MzobTkBuF";

$author_name = "Scott Zeid";
$author_url  = "https://s.zeid.me/";

////////////////////////////////////////////////////////////////

// Copyright (c) 2016, 2018 Scott Zeid.  
// Released under the X11 License:  https://tldrlegal.com/l/x11

$title = [];

if (!empty($simplenote_url)) {
 $html = file_get_contents($simplenote_url);
 $matches = [];
 preg_match("/<div class=\"note( [^\"]*)?\"[^>]*>(.*?)<\\/div>\s*<!--\s*.?note/s", $html, $matches);
 $html = $matches[2];
 $html = preg_replace("/<p id=\"title\"># */", "<p id=\"title\">", $html);
 // Comments are the last line of the quote's list item if it's italicized;
 // however, in order to style them, it's not enough to use `br + em`, since
 // `+` ignores text nodes in between the elements.  Therefore, we add an
 // `after-br` class to any `em` elements that immediately follow br elements
 // and use that class to style the comments.
 $html = preg_replace("/(<br ?\\/?>\s*)<em>/s", "\\1<em class=\"after-br\">", $html);
 preg_match("/<h1>(.*?)<\\/h1>/", $html, $title);
} elseif (!empty($hackpad_url)) {
 $js_url = "$hackpad_url.js?format=html";
 
 $html = file_get_contents($js_url);
 $html = trim($html);
 $html = preg_replace("/^document\.write\\(/", "", $html);
 $html = preg_replace("/\\);$/", "", $html);
 $html = json_decode($html);
 preg_match("/<h1>(.*?)<\\/h1>/", $html, $title);
}

if (count($title))
 $title = html_entity_decode(strip_tags($title[0]), ENT_QUOTES|ENT_HTML5);
else
 $title = "";

?><!DOCTYPE html>

<html>
 <head>
  <meta charset="utf-8">
  <title><?php echo htmlspecialchars($title, ENT_QUOTES|ENT_HTML5); ?></title>
  <meta name="viewport" content="width=device-width; minimum-scale=1; initial-scale=1" />
  <link rel="stylesheet" type="text/css" href="https://s.zeid.me/styles/basic.css" />
  <!--[if lt IE 9]>
   <script type="text/javascript" src="https://s.zeid.me/html5shiv.js"></script>
   <script type="text/javascript" src="https://s.zeid.me/html5shiv-printshiv.js"></script>
  <![endif]-->
  <style type="text/css">
   main p:empty {
    height: 1em;
   }
   main ul + ul[style] {
    margin-top: 0;
   }
   main li + li {
    margin-top: 0.5em;
   }
   main ul.comment {
    padding-left: 1em;
   }
   main ul.comment li,
   main ul li em.after-br:last-child {
    list-style: none;
    font-size: smaller;
    font-style: italic;
    opacity: 0.5;
   }
   main ul li em.after-br:last-child {
    display: inline-block;
    padding-left: 1.25em;
   }
  </style>
 </head>
 <body>
  <header></header>
  <main>
   <article>
    <?php echo $html; echo "\n"; ?>
   </article>
  </main>
  <hr />
  <footer style="text-align: center;">
<?php if (!empty($author_url)): ?>
   <p>
    <i>
     <a href="<?php echo htmlspecialchars($author_url, ENT_QUOTES|ENT_HTML5); ?>">
      <?php echo htmlspecialchars(((!empty($author_name)) ? $author_name : $author_url), ENT_QUOTES|ENT_HTML5)."\n"; ?>
     </a>
    </i>
   </p>
<?php elseif (!empty($author_name)): ?>
   <p>
    <i>
     <?php echo htmlspecialchars($author_name, ENT_QUOTES|ENT_HTML5)."\n"; ?>
    </i>
   </p>
<?php endif ?>
   <p>
    <i>
<?php if (!empty($simplenote_url)): ?>
     <a href="<?php echo htmlspecialchars($simplenote_url, ENT_QUOTES|ENT_HTML5); ?>">
      View on Simplenote
     </a>
<?php elseif (!empty($hackpad_url)): ?>
     <a href="<?php echo htmlspecialchars($hackpad_url, ENT_QUOTES|ENT_HTML5); ?>">
      View on Hackpad
     </a>
<?php endif ?>
<?php if (file_exists(__FILE__."s")): ?>
     &nbsp;ยท&nbsp;
     <a href="<?php echo htmlspecialchars($_SERVER["SCRIPT_NAME"], ENT_QUOTES|ENT_HTML5); ?>s">
      View this page's source code
     </a>
<?php endif ?>
    </i>
   </p>
  </footer>
 </body>
</html>