diff options
author | Thedro Neely <thedroneely@gmail.com> | 2019-03-25 21:27:46 -0400 |
---|---|---|
committer | Thedro Neely <thedroneely@gmail.com> | 2019-03-25 21:27:46 -0400 |
commit | 6f768d766358ac7de862af3082b325e814e0ba45 (patch) | |
tree | ddaaff83a4071cfd7d13024cc1565a9de49ef76b | |
parent | c383d5f68134e49ccdb9448611c8ba3b73977add (diff) | |
download | thedroneely.com-6f768d766358ac7de862af3082b325e814e0ba45.tar.gz thedroneely.com-6f768d766358ac7de862af3082b325e814e0ba45.tar.bz2 thedroneely.com-6f768d766358ac7de862af3082b325e814e0ba45.zip |
app/model/HTMLExtract: Add HMTL Extractor
Extract posts and projects and add to home page
-rw-r--r-- | app/model/HTMLExtract.php | 64 | ||||
-rw-r--r-- | app/views/index.view.php | 66 |
2 files changed, 92 insertions, 38 deletions
diff --git a/app/model/HTMLExtract.php b/app/model/HTMLExtract.php new file mode 100644 index 0000000..5c3f727 --- /dev/null +++ b/app/model/HTMLExtract.php @@ -0,0 +1,64 @@ +<?php + +class DOMExtract extends DOMDocument +{ + private $source; + private $document; + + public function __construct() + { + libxml_use_internal_errors(true); + $this->preserveWhiteSpace = false; + $this->strictErrorChecking = false; + $this->formatOutput = true; + } + + public function setSource($source) + { + $this->source = file_get_contents($source); + return $this; + } + + public function getInnerHTML($tag, $id=null, $nodeValue = false) + { + if (empty($this->source)) { + throw new Exception('Error: Missing $this->source, use setSource() first'); + } + + $html = null; + $this->loadHTML($this->source); + $element = $this->getElementsByTagName($tag); + + foreach ($element as $tags) { + if ($id !== null) { + $attr = explode('=', $id); + if ($tags->getAttribute($attr[0]) == $attr[1]) { + if ($nodeValue == true) { + $html .= trim($tags->nodeValue); + } else { + $html .= $this->innerHTML($tags); + } + } + } else { + if ($nodeValue == true) { + $html .= trim($tags->nodeValue); + } else { + $html .= $this->innerHTML($tags); + } + } + } + return $html; + } + + protected function innerHTML($document) + { + $html = ""; + foreach ($document->childNodes as $v) { + $tmp = new DOMDocument(); + $tmp->appendChild($tmp->importNode($v, true)); + $html .= trim($tmp->saveHTML()); + } + return $html; + } + +} diff --git a/app/views/index.view.php b/app/views/index.view.php index bff5b9c..6c6fb59 100644 --- a/app/views/index.view.php +++ b/app/views/index.view.php @@ -40,53 +40,43 @@ </div> - <div class="columns"> + <div class="recent__article"> - <div class="column has-text-centered has-text-left-mobile"> - <div class="front__textbox has-text-left is-inline-block"> + <h1 class="title has-text-weight-normal">Recent Posts</h1> - <h3 class="has-text-dark is-marginless is-size-5"> - Recent Posts - </h3> + <?php + $recentPosts = new DOMExtract(); + $recentPosts->setSource($_SERVER['DOCUMENT_ROOT'] . '/post/index.html'); + echo $recentPosts->getInnerHTML('recent-article'); + ?> - <?php - $recent_posts = file($_SERVER['DOCUMENT_ROOT'] . '/post/index.html'); - foreach (range(count($recent_posts) - 32, count($recent_posts) - 24) as $line) { - echo $recent_posts[$line]; - } - ?> + <a href="/post/" + class="front__more is-block has-text-right has-text-centered-mobile"> + See All Posts + <?php echo file_get_contents($_SERVER['DOCUMENT_ROOT'] + . '/..' . '/public/css/fonts/feather-icons/chevron-right.svg') ?> + </a> - <br> - <br> - - <a class="front__frame is-inline-block" href="post/">More posts <span class="icon">➤</span></a> - - </div> - </div> - - <div class="column has-text-centered has-text-left-mobile"> - <div class="front__textbox has-text-left is-inline-block"> - - <h3 class="has-text-dark is-marginless is-size-5"> - Recent Projects - </h3> + <br> - <?php - $recent_projects = file($_SERVER['DOCUMENT_ROOT'] . '/post/index.html'); - foreach (range(count($recent_projects) - 15, count($recent_projects) - 7) as $line) { - echo $recent_projects[$line]; - } - ?> + <h1 class="title has-text-weight-normal">Recent Projects</h1> - <br> - <br> + <?php + $recentProjects = new DOMExtract(); + $recentProjects->setSource($_SERVER['DOCUMENT_ROOT'] . '/project/index.html'); + echo $recentProjects->getInnerHTML('recent-article'); + ?> + </div> - <a class="front__frame is-inline-block" href="project/">More projects <span class="icon">➤</span></a> + <a href="/project/" + class="front__more is-block has-text-right has-text-centered-mobile"> + See All Projects + <?php echo file_get_contents($_SERVER['DOCUMENT_ROOT'] + . '/..' . '/public/css/fonts/feather-icons/chevron-right.svg') ?> + </a> - </div> - </div> + <br> - </div> </div> </div> </div> |