just-a-blog

A bad blogging software that converts Markdown into PHP for some reason.
Log | Files | Refs | README | LICENSE

functions.php (3446B)


      1 <?php
      2 
      3 function write_html() {
      4 
      5 	$argc = func_num_args();
      6 	$argv = func_get_args();
      7 
      8 	$output = $argv[0];
      9 
     10 	if( $argc > 0 ) {
     11 		for( $i = 1; $i < $argc; $i++ ) {
     12 			$searchThisPunk = "(\{" . ($i - 1) . "\})";
     13 			if( is_array( $argv[$i] ) ) {
     14 				foreach( $argv[$i] as $thisArg ) {
     15 					$output = preg_replace( $searchThisPunk, $thisArg, $output );
     16 				}
     17 			}
     18 
     19 			else {
     20 				$output = preg_replace( $searchThisPunk, $argv[$i], $output );
     21 			}
     22 		}
     23 	}
     24 
     25 // This is ripe for breaking stuff. Move this into a different function please.
     26 	$output = str_replace( "{allposts}", build_blog(), $output );
     27 
     28 	print( $output );
     29 
     30 }
     31 
     32 function build_blog() {
     33 	
     34 	$posts = get_posts();
     35 	$returnThisPunk = "<ul id=\"listOfPosts\">";
     36 
     37 	foreach( $posts as $date => $thisPost ) {
     38 		$returnThisPunk .= "<li class=\"postSnippet\">\n\t
     39 <span class=\"listTitle\"><a href=\"" . URL . BLOGDIR . "/" . $thisPost . "\">" .
     40 format_name( $thisPost ) . " - <span style=\"postListDate\">" .
     41 date( "Y-m-d", $date ) . "</span></a></span><br />" .
     42 get_post_snippet( $thisPost );
     43 	}
     44 
     45 	$returnThisPunk .= "</ul>";
     46 
     47 	return $returnThisPunk;
     48 
     49 }
     50 
     51 function format_name( $name ) {
     52 
     53 	$returnThisName = preg_replace( "(-)", " ", $name );
     54 	$returnThisName = substr( $returnThisName, 0, strpos( $returnThisName, ".md" ) );
     55 	$returnThisName = ucwords( $returnThisName );
     56 
     57 	return $returnThisName;
     58 
     59 }
     60 
     61 function get_posts() {
     62 	
     63 	$argc = func_num_args();
     64 	$argv = func_get_args();
     65 	
     66 	$posts = preg_grep( '~\.md$~', scandir( "posts/" ) );
     67 	$postDates = array();
     68 	foreach( $posts as $thisPost ) {
     69 		$postDates[] = filectime( "posts/" . $thisPost);
     70 	}
     71 	
     72 	$posts = array_combine( $postDates, $posts );
     73 	krsort( $posts );
     74 
     75 	if( $argc && $argv[0] > 1 ) {
     76 		$posts = array_slice( $posts, 0, $argv[0] );
     77 	}
     78 
     79 	return $posts;
     80 
     81 }
     82 
     83 function get_post_snippet( $post ) {
     84 
     85 	$content = file_get_contents( "posts/" . $post );
     86 	if( strlen( $content ) > 500 ) {
     87 		$shortText = substr($content, 0, strpos( $content, ".", 500) + 1 ); // Short text to first period after 500 characters
     88 	}
     89 	else {
     90 		$shortText = $content;
     91 	}
     92 
     93 	$parsedown = new Parsedown();
     94 
     95 	return strip_tags( $parsedown->text( $shortText ) );
     96 
     97 }
     98 
     99 function link_name( $name ) {
    100 	return str_replace( ".md", "", $name );
    101 }
    102 
    103 function list_all_posts() {
    104 	
    105 	$posts = get_posts();
    106 
    107 	$output = "\n\t<ul id=\"posts\">";
    108 	
    109 	foreach( $posts as $thisPost ) {
    110 		# $output .= "\n\t\t<li><a href=\"" . URL . BLOG_DIR . "/post/" . link_name( $thisPost ) . "\">" . date( "Y-m-d", key( $posts ) ) . " - " . format_name( $thisPost ) . "</a></li>";
    111 		$output .= "\n\t\t<li><a href=\"" . URL . BLOG_DIR . "/index.php?post=" . link_name( $thisPost ) . "\">" . date( "Y-m-d", key( $posts ) ) . " - " . format_name( $thisPost ) . "</a></li>";
    112 	}
    113 
    114 	$output .= "\n\t</ul>";
    115 
    116 	return $output;
    117 
    118 }
    119 
    120 function set_title() {
    121 
    122 	$title = TITLE;
    123 
    124 	if( isset( $_GET['page'] ) ) {
    125 		$thisPage = $_GET['page'];
    126 		$title .= " - " . format_name( $thisPage );
    127 	}
    128 
    129 	return $title;
    130 
    131 }
    132 
    133 function build_nav( $arrayOfPages ) {
    134 
    135 	$nav = "<ul class=\"nav\">\n";
    136 	foreach( $arrayOfPages as $thisPage ) {
    137 		#$nav .= "\t\t\t<li id=\"" . $thisPage . "\">\n\t\t\t\t<a href=\"" . URL . BLOG_DIR . "/page/" . link_name( $thisPage ) . "\">" . format_name( $thisPage ) . "</a></li>\n";
    138 		$nav .= "\t\t\t<li id=\"" . $thisPage . "\">\n\t\t\t\t<a href=\"" . URL . BLOG_DIR . "index.php?page=" . link_name( $thisPage ) . "\">" . format_name( $thisPage ) . "</a></li>\n";
    139 	}
    140 	$nav .= "</ul>";
    141 
    142 	return $nav;
    143 }
    144 
    145 ?>