build.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * <https://y.st./>
  5. * Copyright © 2015-2017 Alex Yst <mailto:copyright@y.st>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  19. **/
  20. /**
  21. * This source code requires PHP to compile. It was tested with PHP
  22. * 5.6.6-2, but will likely work with anything at least as recent as
  23. * PHP 5.3. Any PHP version older than that lacks name spaces, and will
  24. * not work with this code. To install PHP without installing a Web
  25. * server, install the "php5-cli" on Debian. The package may be under a
  26. * different name on other systems and distributions. Additionally, you
  27. * will need PECL's gnupg_*() functions.
  28. *
  29. * sudo aptitude install php5-cli php5-gnupg
  30. *
  31. * You will need to install the include.d PHP library, which is not
  32. * available from the Debian repository. It can be found at
  33. * <https://notabug.org./y.st./include.d>.
  34. **/
  35. // Used for end-of-run statistics output
  36. $time_script_started = time();
  37. // A little setup ...
  38. spl_autoload_register();
  39. require 'st/y/function/error_handler.php';
  40. require 'st/y/function/highlight_string.php';
  41. require 'st/y/function/recurse_dir.php';
  42. require 'st/y/function/update_file.php';
  43. set_error_handler('\\st\\y\\error_handler');
  44. require __DIR__.'/source/variables.php';
  45. require __DIR__.'/source/closure.php';
  46. // If invalid arguments are used, the whole compilation process should
  47. // halt and an error message should be presented.
  48. if(isset($invalid_arguments)):
  49. die("\nInvalid arguments: ".implode(' ', $invalid_arguments)."\n\n");
  50. endif;
  51. // If the canary chirps ...
  52. if(in_array('--canary', $argv)):
  53. ob_start();
  54. require __DIR__.'/source/canary.php';
  55. $canary = $pgp->sign(ob_get_clean());
  56. \st\y\update_file(__DIR__.'/static/a/canary.txt', $canary);
  57. else:
  58. echo "\nYou did not run this script using the \"--canary\" flag. Is everything okay?\n\n";
  59. endif;
  60. // Tell PHP to use detached signatures, so the XHTML pages will still validate.
  61. $pgp->setsignmode(GNUPG_SIG_MODE_DETACH);
  62. // Some files are static, and only need to be copied, not compiled.
  63. foreach(\st\y\recurse_dir(__DIR__.'/static') as $filename):
  64. $dirname = dirname(__DIR__."/compiled/$filename");
  65. if(!is_dir($dirname)):
  66. mkdir($dirname, 0777, true);
  67. endif;
  68. \st\y\update_file(__DIR__."/compiled/$filename", file_get_contents(__DIR__."/static/$filename"));
  69. $should_exist[$filename] = true;
  70. endforeach;
  71. // Other files are compiled.
  72. foreach(\st\y\recurse_dir(__DIR__."/source/pages") as $filename):
  73. require __DIR__."/source/pages/$filename";
  74. $¢_build_page($filename, $xhtml);
  75. endforeach;
  76. // Account for moved pages.
  77. require __DIR__.'/source/redirect.php';
  78. // Directory indexes should be built.
  79. require __DIR__.'/source/weblog_index.php';
  80. // Copy the English index to the main index because the plan to set up
  81. // an Esperanto version of the website sort of flopped.
  82. copy(__DIR__.'/compiled/en/index.xhtml', __DIR__.'/compiled/index.xhtml');
  83. $should_exist['index.xhtml'] = true;
  84. foreach(\st\y\recurse_dir(__DIR__.'/compiled') as $filename):
  85. if(!isset($should_exist[$filename])):
  86. unlink(__DIR__."/compiled/$filename");
  87. endif;
  88. endforeach;
  89. // End-of-run statistics:
  90. $time_until_script_ended = time() - $time_script_started;
  91. echo "The script completed in $time_until_script_ended seconds.\n";