In this repo i store all my websites, each in a different branch
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

186 linhas
4.3 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: mhaaz
  5. * Date: 11/24/2017
  6. * Time: 11:02 AM
  7. */
  8. use Michelf\Markdown;
  9. function limit_text($text, $limit) {
  10. if (str_word_count($text, 0) > $limit) {
  11. $words = str_word_count($text, 2);
  12. $pos = array_keys($words);
  13. $text = substr($text, 0, $pos[$limit]) . '...';
  14. }
  15. return $text;
  16. }
  17. /* General Blog Functions */
  18. function get_post_names(){
  19. static $_cache = array();
  20. if(empty($_cache)){
  21. // Get the names of all the
  22. // posts (newest first):
  23. $_cache = array_reverse(glob('posts/*.md'));
  24. }
  25. return $_cache;
  26. }
  27. function get_posts($page = 1, $perpage = 0){
  28. if($perpage == 0){
  29. $perpage = 6;
  30. }
  31. $posts = get_post_names();
  32. // Extract a specific page with results
  33. $posts = array_slice($posts, ($page-1) * $perpage, $perpage);
  34. $tmp = array();
  35. // Create a new instance of the markdown parser
  36. $md = new Markdown();
  37. foreach($posts as $k=>$v){
  38. $post = new stdClass;
  39. // Extract the date
  40. $arr = explode('_', $v);
  41. $post->date = strtotime(str_replace('posts/','',$arr[0]));
  42. // The post URL
  43. $post->url = '/'.date('Y/m', $post->date).'/'.str_replace('.md','',$arr[1]);
  44. // Get the contents and convert it to HTML
  45. $content = $md->defaultTransform(file_get_contents($v));
  46. // Extract the title and body
  47. $arr = explode('</h1>', $content);
  48. $post->title = str_replace('<h1>','',$arr[0]);
  49. $post->body = $arr[1];
  50. $tmp[] = $post;
  51. }
  52. return $tmp;
  53. }
  54. function find_posts($page = 1, $perpage = 0){
  55. if($perpage == 0){
  56. $perpage = 3;
  57. }
  58. $posts = get_post_names();
  59. // Extract a specific page with results
  60. $posts = array_slice($posts, ($page-1) * $perpage, $perpage);
  61. $tmp = array();
  62. // Create a new instance of the markdown parser
  63. $md = new Markdown();
  64. foreach($posts as $k=>$v){
  65. $post = new stdClass;
  66. // Extract the date
  67. $arr = explode('_', $v);
  68. $post->date = strtotime(str_replace('posts/','',$arr[0]));
  69. // The post URL
  70. $post->url = '/'.date('Y/m', $post->date).'/'.str_replace('.md','',$arr[1]);
  71. // Get the contents and convert it to HTML
  72. $content = $md->defaultTransform(file_get_contents($v));
  73. // Extract the title and body
  74. $arr = explode('</h1>', $content);
  75. $post->title = str_replace('<h1>','',$arr[0]);
  76. $post->body = limit_text(strip_tags($arr[1]),30);
  77. $tmp[] = $post;
  78. }
  79. return $tmp;
  80. }
  81. function random_posts(){
  82. $page = 1;
  83. $perpage = 10;
  84. $posts = get_post_names();
  85. shuffle($posts);
  86. // Extract a specific page with results
  87. $posts = array_slice($posts, 4);
  88. $tmp = array();
  89. // Create a new instance of the markdown parser
  90. $md = new Markdown();
  91. foreach($posts as $k=>$v){
  92. $post = new stdClass;
  93. // Extract the date
  94. $arr = explode('_', $v);
  95. $post->date = strtotime(str_replace('posts/','',$arr[0]));
  96. // The post URL
  97. $post->url = '/'.date('Y/m', $post->date).'/'.str_replace('.md','',$arr[1]);
  98. // Get the contents and convert it to HTML
  99. $content = $md->defaultTransform(file_get_contents($v));
  100. // Extract the title and body
  101. $arr = explode('</h1>', $content);
  102. $post->title = str_replace('<h1>','',$arr[0]);
  103. $tmp[] = $post;
  104. }
  105. return $tmp;
  106. }
  107. // Find post by year, month and name
  108. function find_post($year, $month, $name){
  109. foreach(get_post_names() as $index => $v){
  110. if( strpos($v, "$year-$month") !== false && strpos($v, $name.'.md') !== false){
  111. // Use the get_posts method to return
  112. // a properly parsed object
  113. $arr = get_posts($index+1,1);
  114. return $arr[0];
  115. }
  116. }
  117. return false;
  118. }
  119. // Helper function to determine whether
  120. // to show the pagination buttons
  121. function has_pagination($page = 1){
  122. $total = count(get_post_names());
  123. $pages = ceil($total/3);
  124. return array(
  125. 'pages'=> $pages
  126. );
  127. }
  128. // Turn an array of posts into a JSON
  129. function generate_json($posts){
  130. return json_encode($posts);
  131. }