Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.54 KB | None | 0 0
  1. <?php
  2. /**
  3. * Table of Contents HTML Generator
  4. *
  5. * File: toc.php
  6. * Created: 2019-03-19
  7. * Updated: 2019-03-23 RDOS
  8. * Time: 09:09 EDT
  9. */
  10.  
  11. // namespace
  12.  
  13. /**
  14. * Table of Contents HTML Generator Class
  15. *
  16. * Generates a generic table of contents from the directory structure starting
  17. * at the root of the directory at which it is pointed. Limited to a certain
  18. * depth, a certain number of files and a printed file length (all set in
  19. * options). Only picks up HTML files (options).
  20. */
  21. class TocGenerator
  22. {
  23. /** Options */
  24. protected $opts = [
  25. 'page' => [
  26. 'title' => 'Table of Contents',
  27. 'lang' => 'en-CA',
  28. 'robots' => false,
  29. 'cols' => 3,
  30. ],
  31. 'max' => [
  32. 'depth' => 4,
  33. 'files' => [
  34. 'd0' => 7,
  35. 'd1' => 72,
  36. 'd2' => 12,
  37. 'd3' => 14,
  38. ],
  39. ],
  40. 'dir' => [
  41. 'cache' => '/a',
  42. 'meta' => '/a/0',
  43. 'toc' => '/a/0/toc',
  44. ],
  45. 'security' => [ 'file' => '.security', ],
  46. 'file' => [
  47. 'ext' => '.html',
  48. 'name' => '/article.html',
  49. 'print' => true,
  50. 'display' => true,
  51. 'size' => [ 'max' => 10000 ],
  52. ],
  53. 'toc' => [ 'file' => '/default.html' ],
  54. 'class' => [
  55. 'div' => 'toc',
  56. 'list' => '',
  57. ],
  58. 'debug' => true,
  59. ];
  60.  
  61. /**
  62. * Initialize the Class
  63. *
  64. * @return void
  65. */
  66. public function init()
  67. {
  68. $security = $this->check();
  69.  
  70. if ( $security )
  71. {
  72. $this->run();
  73. }
  74. // else do nothing (no hints).
  75. }
  76.  
  77. /**
  78. * Security Check
  79. *
  80. * Check for a hidden file on the server. Only run if this is present. This
  81. * is to prevent unintended or intentional but malevolent execution of this
  82. * script. This isn't desirable as it runs through the entire cached directory
  83. * and could get quite large. This file may be a good candidate for a daily
  84. * or weekly cron job (turn off if no changes over prolonged periods). Could
  85. * provide a hint to this effect in the page displayed if calling this file
  86. * directly. The security file should be empty. It does not need to contain
  87. * anything, it just needs to be there.
  88. */
  89. private function check()
  90. {
  91. $file = $this->opts['security']['file'];
  92.  
  93. if( file_exists( __DIR__ . '/' . $file ) )
  94. {
  95. return true;
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102.  
  103. /**
  104. * Run the Class
  105. *
  106. * Uses a hook and chain approach (Top Down)
  107. */
  108. private function run()
  109. {
  110. // Get the paths needed for this work.
  111. $paths = $this->getPaths();
  112.  
  113. //Initialize
  114. $arr = [];
  115.  
  116. // Check to ensure that $paths is an array and not empty.
  117. if( is_array( $paths ) && count( $paths ) > 0 )
  118. {
  119. // Iterate through the directories.
  120. $arr = $this->itrDir00( $paths['cache'] );
  121.  
  122. // Create the div to hold the contents.
  123. $html = $this->getDivHtml( $paths, $arr );
  124.  
  125. // Check to make sure we have what we need.
  126. if( is_string( $html ) && strlen( $html ) > 0 )
  127. {
  128. // Get the page meta to describe what the page is about.
  129. $meta = $this->getPageMeta();
  130.  
  131. // Add the page meta to the _end_ of the HTML just generated.
  132. $html = $html . $meta;
  133.  
  134. // Wrap the HTML just generated in Page HTML
  135. $page = $this->getPageHtml( $html );
  136.  
  137. // Build the file path and name we need to print it to a file.
  138. $file = $paths['toc'] . $this->opts['toc']['file'];
  139.  
  140. // Print the file and obtain the response.
  141. $resp = $this->printHTML( $file, $page );
  142.  
  143. pre_dump( $resp );
  144. }
  145. else
  146. {
  147. 'No HTML';
  148. }
  149. }
  150. }
  151.  
  152. /**
  153. * Build the TOC portion of the HTML page
  154. *
  155. * Note: No closing tag is needed on the list element (ref).
  156. */
  157. private function getDivHtml( $paths, $list )
  158. {
  159. // Check to ensure it is an array and that it is not empty
  160. if ( is_string( $list ) && strlen( $list ) > 0 )
  161. {
  162. // Initialize
  163. $str = '';
  164. // Get the class for the top level div.
  165. $class = $this->getClass( $this->opts['class']['div'] );
  166.  
  167. $str .= '<style>' . PHP_EOL;
  168. $str .= '.toc ul { list-style: none; }' . PHP_EOL;
  169. $str .= '.toc section {' . PHP_EOL;
  170. $str .= ' margin: 15px 0;' . PHP_EOL;
  171. $str .= sprintf( ' columns: %s;', $this->opts['page']['cols'] );
  172. $str .= '}' . PHP_EOL;
  173. $str .= '</style>' . PHP_EOL;
  174.  
  175. // Create the title elements
  176. $str .= sprintf( '<h1>%s</h1>%s', $this->opts['page']['title'], PHP_EOL );
  177.  
  178. // open the div
  179. $str .= sprintf( '<div%s>%s', $class, PHP_EOL );
  180.  
  181. $str .= $list;
  182.  
  183. // Close the div
  184. $str .= '</div>' . PHP_EOL;
  185.  
  186. return $str;
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193.  
  194. /**
  195. * Get the List UL
  196. *
  197. * @param $arr Array containing the items from which to generate list
  198. *
  199. * @return string Unordered list
  200. */
  201. private function getListHtml( $paths, $arr )
  202. {
  203. // Initialize
  204. $str = '';
  205. $cnt = 0;
  206. // Open the list element (<ul>), and leave room for adding classes.
  207. $str .= sprintf( '<ul>%s', PHP_EOL );
  208.  
  209. // This loop calls the containing function if the value encountered is an array.
  210. foreach( $arr as $val )
  211. {
  212. if ( is_array( $val ) )
  213. {
  214. // Trailing </li> removed...
  215. if( 0 == $cnt )
  216. {
  217. $str .= '<li>' . getListUL( $val, $str );
  218. }
  219. else
  220. {
  221. $str .= PHP_EOL . '<li>' . getListUL( $val, $str );
  222. }
  223. }
  224. else
  225. {
  226. $str .= sprintf( '<li><a href="%s">%s</a>%s', $val, $val, PHP_EOL );
  227. }
  228. $cnt++;
  229. }
  230.  
  231. $str .= '</ul>' . PHP_EOL;
  232.  
  233. return $str;
  234. }
  235.  
  236. /**
  237. * Get the Class
  238. *
  239. * Get the class, or return empty if none available
  240. *
  241. * @param string $class
  242. *
  243. * @return string String containing 'class="class"'.
  244. */
  245. private function getClass( $class )
  246. {
  247. // Initialize
  248. $str = '';
  249.  
  250. // build the class string (simple).
  251. if ( is_string( $class ) && strlen( $class ) > 0 ) {
  252. $str .= sprintf( ' class="%s"', $class );
  253. }
  254.  
  255. return $str;
  256. }
  257.  
  258. /**
  259. * Scan Directory (Simple)
  260. *
  261. */
  262. function scanDirToList( $path ) {
  263.  
  264. $arr = [];
  265.  
  266. $arrDir = scandir( $path );
  267.  
  268. // Initilize
  269. $str = '';
  270.  
  271. $str .= '<ul>' . PHP_EOL;
  272.  
  273. foreach ( $arrDir as $key => $value )
  274. {
  275. if ( ! in_array( $value, array( '.', '..' ) ) )
  276. {
  277. if ( is_dir( $dir . '/' . $value ) )
  278. {
  279. $result[$value] = scanDirToList( $dir . '/' . $value );
  280. }
  281. else
  282. {
  283. $result[] = $value;
  284. }
  285. }
  286. }
  287.  
  288. return $result;
  289. }
  290.  
  291. /**
  292. * Iterate Directory 00
  293. *
  294. * Use the basic Directory Iterator class to scan just the directory in which
  295. * it is, then manually detemine if it has discovered a directory. Create
  296. * the list item HTML as we go along.
  297. *
  298. * '.': Current directory
  299. * '..': Parent directory
  300. *
  301. * @param array $paths
  302. *
  303. * @return string
  304. */
  305. private function itrDir00( $path0 )
  306. {
  307. // Initialize.
  308. $str = '';
  309.  
  310. // Set the count to 0.
  311. $cnt = 0;
  312.  
  313. // Set the max depth allowed.
  314. $max['depth'] = $this->opts['max']['depth'];
  315.  
  316. // Set the max files allowed for depth 0 (root of cache).
  317. $max['files'] = $this->opts['max']['files']['d0'];
  318.  
  319. // Set the number of columns.
  320. $cols = $this->opts['page']['cols'];
  321.  
  322. // Wrap the list in a section, with a class.
  323. $str .= '<section class="list columns">' . PHP_EOL;
  324.  
  325. // Open the list element.
  326. $str .= '<ul>' . PHP_EOL;
  327.  
  328. // Scan just the directory given, via the full path.
  329. $files = scandir( $path0 );
  330.  
  331. // Loop through the files obtained.
  332. foreach ( $files as $file )
  333. {
  334. // Check the file (pass it through a filter).
  335. if ( $file = $this->chkFile00( $file ) )
  336. {
  337. // Make sure the limit has not been exceeded.
  338. if ( $cnt < $max['files'] )
  339. {
  340. // Check to see if this is a directory.
  341. if( is_dir( $path0 . '/' . $file ) )
  342. {
  343. // Obtain just the slug (for the link).
  344. $slug = $this->opts['dir']['cache'] . '/' . $file;
  345.  
  346. // Create the list item. Add an id for a bookmark.
  347. $str .= sprintf( '<li><a id="%s" href="%s/">%s</a></li>%s',
  348. $file, $slug, ucfirst( $file ), PHP_EOL );
  349.  
  350. // Create the path for the next level.
  351. $path1 = $path0 . '/' . $file;
  352.  
  353. // Create a nav item after the fact to attach to the top of the str.
  354. $arrNav[$cnt]['dir'] = $this->opts['dir']['cache'];
  355. $arrNav[$cnt]['file'] = $file;
  356. $arrNav[$cnt]['base'] = $this->opts['dir']['toc'];
  357. $arrNav[$cnt]['slug'] = $slug;
  358. $arrNav[$cnt]['path'] = $path1;
  359. $arrNav[$cnt]['title'] = ucfirst( $file );
  360.  
  361. // Scan the next path given to see if it is not empty.
  362. $files1 = scandir( $path1 );
  363.  
  364. // Check to see if it is not empty.
  365. if( count( $files1 ) > 2 )
  366. {
  367. // Create the HTML from the next path.
  368. $strDir1 = $this->itrDir01( $slug, $path1 );
  369.  
  370. if ( $strDir1 )
  371. {
  372. $str .= $strDir1;
  373. }
  374. }
  375.  
  376. // Increment the counter.
  377. $cnt++;
  378. } // Continue to the next item.
  379. }
  380. else
  381. {
  382. // Maximum files reached for this level. Break.
  383. break;
  384. }
  385. } // Continue to the next item.
  386. } // Done the loop.
  387.  
  388. // Close the list element.
  389. $str .= '</ul>' . PHP_EOL;
  390.  
  391. // Close the section.
  392. $str .= '</section>' . PHP_EOL;
  393.  
  394. // Build the nav HTML
  395. $strNav = $this->getNavHtml( $arrNav );
  396.  
  397. // Attach the Nav HTML to the TOP of the string built so far.
  398. $str = $strNav . $str;
  399.  
  400. // Return the string.
  401. return $str;
  402. }
  403.  
  404. /**
  405. * Iterate Directory 01
  406. *
  407. * Scan the Second Level Directories.
  408. *
  409. * '.': Current directory
  410. * '..': Parent directory
  411. *
  412. * @param array $paths
  413. *
  414. * @return string
  415. */
  416. private function itrDir01( $slug0, $path1 )
  417. {
  418. if ( is_string( $path1 ) && strlen( $path1 ) > 0 )
  419. {
  420. // Initialize.
  421. $str = '';
  422.  
  423. // Set the count to 0.
  424. $cnt = 0;
  425.  
  426. // Set the max depth allowed.
  427. $max['depth'] = $this->opts['max']['depth'];
  428.  
  429. // Set the max files allowed.
  430. $max['files'] = $this->opts['max']['files']['d1'];
  431.  
  432. // Open the list element.
  433. $str .= '<ul>' . PHP_EOL;
  434.  
  435. // Scan just the directory given
  436. $files = scandir( $path1 );
  437.  
  438. $itrDir1 = new DirectoryIterator( $path1 );
  439.  
  440. // Loop through the files obtained.
  441. foreach ( $files as $file )
  442. {
  443. // Check the file (pass it through a filter).
  444. if ( $file = $this->chkFile01( $file ) )
  445. {
  446. // Make sure the limit has not been exceeded.
  447. if ( $cnt < $max['files'] )
  448. {
  449. // Check to see if this is a directory.
  450. if( is_dir( $path1 . '/' . $file ) )
  451. {
  452. // Obtain just the slug (for the link).
  453. $slug1 = $slug0 . '/' . $file;
  454.  
  455. // Create the file name to check if it exists in current directory
  456. $isFile = $path1 . '/' . $file . $this->opts['file']['name'];
  457.  
  458. $star = $this->isStarred( $isFile );
  459.  
  460. // Create the list item
  461. $str .= sprintf( '<li><a href="%s/">%s%s</a></li>%s',
  462. $slug1, ucfirst( $file ), $star, PHP_EOL );
  463.  
  464. // Now this directory needs to be checked for subdirectories.
  465. // $dir2 = scandir( $file );
  466.  
  467. // Increment the counter.
  468. $cnt++;
  469. } // Continue to the next item.
  470. }
  471. else
  472. {
  473. // Maximum files reached for this level. Break.
  474. break;
  475. }
  476. } // Continue to the next item.
  477. } // Done the loop.
  478.  
  479. // Close the list element.
  480. $str .= '</ul>' . PHP_EOL;
  481.  
  482. // Return the string.
  483. return $str;
  484. }
  485. else
  486. {
  487. return false;
  488. }
  489. }
  490.  
  491. /**
  492. * Check if file Exists to Star or Not
  493. *
  494. * Check if a specific file exists to determine whether or not to give it a
  495. * "star" (*). The star (*) indicates the presence of readable content.
  496. *
  497. * @param string $file
  498. *
  499. * @return string star (*) or empty string ''.
  500. */
  501. private function isStarred( $file )
  502. {
  503. // Initialize
  504. $star = '';
  505.  
  506. // Check to ensure the file given is a string and of reasonable length.
  507. if ( is_string( $file ) && strlen( $file ) > 2 )
  508. {
  509. // If it exists, set the star variable to '*';
  510. if( file_exists( $file ) )
  511. {
  512. $star = '*';
  513. }
  514. }
  515.  
  516. // Return the star.
  517. return $star;
  518. }
  519.  
  520. /**
  521. * Iterate Directory 01
  522. *
  523. * Scan the Second Level Directories.
  524. *
  525. * '.': Current directory
  526. * '..': Parent directory
  527. *
  528. * @param array $paths
  529. *
  530. * @return string
  531. */
  532. private function itrDir02( $slug1, $dir2 )
  533. {
  534. if ( is_string( $dir1 ) && strlen( $dir1 ) > 0 )
  535. {
  536. // Initialize.
  537. $str = '';
  538.  
  539. // Set the count to 0.
  540. $cnt = 0;
  541.  
  542. // Set the max depth allowed.
  543. $max['depth'] = $this->opts['max']['depth'];
  544.  
  545. // Set the max files allowed.
  546. $max['files'] = $this->opts['max']['files']['d1'];
  547.  
  548. // Open the list element.
  549. $str .= '<ul>' . PHP_EOL;
  550.  
  551. // Scan just the directory given
  552. $files = scandir( $dir1 );
  553.  
  554. $itrDir1 = new DirectoryIterator( $dir1 );
  555.  
  556. // Loop through the files obtained.
  557. foreach ( $files as $file )
  558. {
  559. // Check the file (pass it through a filter).
  560. if ( $file = $this->chkFile01( $file ) )
  561. {
  562. // Make sure the limit has not been exceeded.
  563. if ( $cnt < $max['files'] )
  564. {
  565. // Check to see if this is a directory.
  566. if( is_dir( $dir1 . '/' . $file ) )
  567. {
  568. // Obtain just the slug (for the link).
  569. $slug = $slug0 . '/' . $file;
  570.  
  571. // Create the list item
  572. $str .= sprintf( '<li><a href="%s/">%s</a></li>%s', $slug, ucfirst( $file ), PHP_EOL );
  573.  
  574. // Now this directory needs to be checked for subdirectories.
  575. // $dir2 = scandir( $file );
  576.  
  577. // Increment the counter.
  578. $cnt++;
  579. } // Continue to the next item.
  580. }
  581. else
  582. {
  583. // Maximum files reached for this level. Break.
  584. break;
  585. }
  586. } // Continue to the next item.
  587. } // Done the loop.
  588.  
  589. // Close the list element.
  590. $str .= '</ul>' . PHP_EOL;
  591.  
  592. // Return the string.
  593. return $str;
  594. }
  595. else
  596. {
  597. return false;
  598. }
  599. }
  600.  
  601. /**
  602. * Check Files in the Top Level Directory
  603. *
  604. * Return only files that are three characters long (for this level).
  605. * Exclude files that are not wanted that are three characters long.
  606. *
  607. * @param string $file
  608. *
  609. * @return string [$file]
  610. */
  611. private function chkFile00( $file )
  612. {
  613. // Basic check
  614. if ( is_string( $file ) && strlen( $file ) > 0 )
  615. {
  616. // automatically rejects single and double dots, and 1 letter directories.
  617. if ( strlen( $file ) > 2 && strlen( $file ) < 4 )
  618. {
  619. // Get excluded directories.
  620. $exclude = $this->getExc00();
  621.  
  622. // Return the file, having passed through this filter (Made it!!!)
  623. if ( ! in_array( $file, $exclude ) )
  624. {
  625. return $file;
  626. }
  627. else
  628. {
  629. return false;
  630. }
  631. }
  632. else
  633. {
  634. return false;
  635. }
  636. }
  637. else
  638. {
  639. return false;
  640. }
  641. }
  642.  
  643. /**
  644. * Check Files in the Second Level Directory
  645. *
  646. *
  647. * @param string $file
  648. *
  649. * @return string [$file]
  650. */
  651. private function chkFile01( $file )
  652. {
  653. // Basic check
  654. if ( is_string( $file ) && strlen( $file ) > 0 )
  655. {
  656. // automatically rejects single and double dots, and 1 letter directories.
  657. if ( strlen( $file ) > 3 && strlen( $file ) < 5 )
  658. {
  659. // Get excluded directories.
  660. $exclude = $this->getExc01();
  661.  
  662. // Return the file, having passed through this filter (Made it!!!)
  663. if ( ! in_array( $file, $exclude ) )
  664. {
  665. return $file;
  666. }
  667. else
  668. {
  669. return false;
  670. }
  671. }
  672. else
  673. {
  674. return false;
  675. }
  676. }
  677. else
  678. {
  679. return false;
  680. }
  681. }
  682.  
  683. /**
  684. * Get Excluded Directories
  685. */
  686. private function getExc00()
  687. {
  688. $arr = [ '.', '..', 'cmn', 'mem', 'wki' ];
  689. return $arr;
  690. }
  691.  
  692. /**
  693. * Get Excluded Directories
  694. */
  695. private function getExc01()
  696. {
  697. $arr = [ '.', '..', 'cmn', 'mem', ];
  698. return $arr;
  699. }
  700.  
  701. /**
  702. * Get the Paths
  703. *
  704. * @return string
  705. */
  706. private function getPaths()
  707. {
  708. // Initialize.
  709. $paths = [];
  710.  
  711. // Path to the root of the site
  712. $paths['root'] = null;
  713.  
  714. // Path to the cache (i.e. SITE_ROOT_PATH . '/a')
  715. $paths['cache'] = null;
  716.  
  717. // Path to the toc (i.e. SITE_CACHE_PATH . '/0/toc')
  718. $paths['toc'] = null;
  719.  
  720. // Determine the root path by removing the trailing part.
  721. $paths['root'] = str_replace( $this->opts['dir']['toc'], '', __DIR__ );
  722.  
  723. // Create the path to the cache by adding the cache directory to the root.
  724. $paths['cache'] = $paths['root'] . $this->opts['dir']['cache'];
  725.  
  726. // Create the path to the TOC by adding the TOC directory to the root.
  727. $paths['toc'] = $paths['root'] . $this->opts['dir']['toc'];
  728.  
  729. return $paths;
  730. }
  731.  
  732. /**
  733. * Get the Page Meta
  734. *
  735. * Information to describe what the page is about.
  736. *
  737. * @return string
  738. */
  739. private function getPageMeta()
  740. {
  741. // Initialize.
  742. $str = '';
  743.  
  744. $str .= '<section class="meta">' . PHP_EOL;
  745. $str .= '<h2>About this Page</h2>' . PHP_EOL;
  746. $str .= '<p>The page is automatically generated. It uses a script to iterate' . PHP_EOL;
  747. $str .= ' through the directory structure created. This is different than' . PHP_EOL;
  748. $str .= ' generating it from a database. A directory structure is part of' . PHP_EOL;
  749. $str .= ' the file system of an operating system (OS) and so is more robust' . PHP_EOL;
  750. $str .= ' than that created from a database. It is also easier to work with' . PHP_EOL;
  751. $str .= ' and can be set up by anyone used to folders on common operating' . PHP_EOL;
  752. $str .= ' systems (i.e. Mac, Windows or Linux).' . PHP_EOL;
  753. $str .= '</section>' . PHP_EOL;
  754.  
  755. return $str;
  756. }
  757.  
  758. /**
  759. * Build the Complete HTML Page
  760. *
  761. * @param $html
  762. *
  763. * @return string
  764. */
  765. private function getPageHtml( $html )
  766. {
  767. $str = '<!DOCTYPE html>' . PHP_EOL;
  768. $str .= sprintf( '<html lang="%s">%s', $this->opts['page']['lang'], PHP_EOL );
  769. $str .= '<head>' . PHP_EOL;
  770. $str .= '<meta charset="UTF-8">' . PHP_EOL;
  771. $str .= '<meta name="viewport" content="width=device-width, initial-scale=1"/>' . PHP_EOL;
  772. $str .= sprintf( '<title>%s</title>%s', $this->opts['page']['title'], PHP_EOL );
  773. $str .= $this->opts['page']['robots'] ? '<meta name="robots" content="noindex,nofollow" />' . PHP_EOL : '';
  774. $str .= sprintf( '<link rel=stylesheet href="/style.min.css">%s', PHP_EOL);
  775. $str .= '</head>' . PHP_EOL;
  776. $str .= '<body>' . PHP_EOL;
  777. $str .= '<main>' . PHP_EOL;
  778. // open the article element
  779. $str .= '<article>' . PHP_EOL;
  780. $str .= $html;
  781. // close the article element
  782. $str .= '</article>' . PHP_EOL;
  783. $str .= '</main>' . PHP_EOL;
  784. $str .= '<nav class="nav-up"><a href="../" title="Go up one directory">^</a></nav>' . PHP_EOL;
  785. $str .= '</html>' . PHP_EOL;
  786.  
  787. return $str;
  788. }
  789.  
  790. /**
  791. * Nav HTML
  792. *
  793. * Builds the Nav HTML from the top level directories
  794. * #arrNav[]['dir'] = $dir0;
  795. * $arrNav[]['file'] = $file;
  796. * $arrNav[]['slug'] = $slug;
  797. * $arrNav[]['path'] = $path1;
  798. * $arrNav[]['title'] = ucfirst( $file );
  799. *
  800. * @param array $nav
  801. *
  802. * @return string
  803. */
  804. private function getNavHtml( $arr )
  805. {
  806. // Check to see if it is an array and that it is not empty.
  807. if ( is_array( $arr ) && count( $arr ) > 0 )
  808. {
  809. // Initialize the string
  810. $str = '';
  811.  
  812. // Open the nav element.
  813. $str .= '<nav>' . PHP_EOL;
  814.  
  815. // Loop through the array items
  816. foreach ( $arr as $item )
  817. {
  818. $str .= sprintf( '<a href="%s/#%s" title="%s">%s</a> &nbsp;&nbsp;',
  819. $item['base'], $item['file'], $item['title'], $item['title'] );
  820. }
  821.  
  822. // Close the nav element.
  823. $str .= '</nav>' . PHP_EOL;
  824. }
  825.  
  826. // Return the string.
  827. return $str;
  828. }
  829.  
  830. /**
  831. * Print the HTML to a File
  832. *
  833. * Checks the length of the string
  834. *
  835. * file['path']: path to the file
  836. * file['stub']: first part of the file name
  837. * file['ext']: extension (if needed)
  838. * file['name']: first and second part of the file name.
  839. * file['title']: file stub exploded and capitalized as needed.
  840. * file['size']: file size
  841. * file['date']['created']: file created date
  842. * file['date']['updated']: file last updated
  843. * file['author]['initials']: author initials (may be in article meta info).
  844. *
  845. */
  846. private function printHTML( $file, $html )
  847. {
  848. // Initialize.
  849. $resp = null;
  850.  
  851. if (
  852. $this->opts['file']['print']
  853. && strlen( $html ) > 0
  854. && strlen( $html ) < $this->opts['file']['size']['max']
  855. )
  856. {
  857. $resp = file_put_contents( $file, $html );
  858. }
  859.  
  860. return $resp;
  861. }
  862.  
  863. } // end class
  864.  
  865. if( ! function_exists( 'pre_dump' ) ) {
  866. function pre_dump( $arr ) {
  867. if ( isset( $_GET['display'] ) )
  868. {
  869. echo '<pre>';
  870. var_dump( $arr );
  871. echo '</pre>';
  872. }
  873. }
  874. }
  875.  
  876. /**
  877. * Table of Contents Generator public function
  878. *
  879. * Instantiates a new class, then calls the init function in it.
  880. *
  881. * @return void
  882. */
  883. function toc_generator()
  884. {
  885. $toc = new TocGenerator();
  886. $toc->init();
  887. }
  888. toc_generator();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement