~hxii/saisho

65359e614cc0236272dfaaeda665b2e426f5e9af — hxii 3 months ago c4dc854
Position Sorting
2 files changed, 80 insertions(+), 48 deletions(-)

M index.php
M template/home.php
M index.php => index.php +33 -1
@@ 79,7 79,7 @@ class Saisho {
  public function get_page( string $page, bool $metadata_only = false ) {
    if ( file_exists( DATA_FOLDER . DIRECTORY_SEPARATOR . $page ) ) {
      if ( $metadata_only ) {
        return $this->parse_page( DATA_FOLDER . DIRECTORY_SEPARATOR . $page, false );
        return $this->parse_page( DATA_FOLDER . DIRECTORY_SEPARATOR . $page, false ); 
      }
      return $this->parse_page( DATA_FOLDER . DIRECTORY_SEPARATOR . $page );
    }


@@ 245,26 245,58 @@ class Saisho {
      } else {
        uasort( $files, array( $this, 'sort_by_title_asc' ) );
      }
    } elseif ( $sortby === 'by_position' ) {
      if ( $order === 'desc' ) {
        uasort( $files, array( $this, 'sort_by_position_desc' ) );
      } else {
        uasort( $files, array( $this, 'sort_by_position_asc' ) );
      }
    }
    return $files;
  }

  function sort_by_date_desc( $a, $b ) {
    if ( ! (isset($a['metadata']['date']) && isset($b['metadata']['date']) ) ) {
      return 0;
    }
    return $a['metadata']['date'] < $b['metadata']['date'];
  }

  function sort_by_date_asc( $a, $b ) {
    if ( ! (isset($a['metadata']['date']) && isset($b['metadata']['date']) ) ) {
      return 0;
    }
    return $a['metadata']['date'] > $b['metadata']['date'];
  }

  function sort_by_title_desc( $a, $b ) {
    if ( ! (isset($a['metadata']['title']) && isset($b['metadata']['title']) ) ) {
      return 0;
    }
    return $a['metadata']['title'] < $b['metadata']['title'];
  }

  function sort_by_title_asc( $a, $b ) {
    if ( ! (isset($a['metadata']['title']) && isset($b['metadata']['title']) ) ) {
      return 0;
    }
    return $a['metadata']['title'] > $b['metadata']['title'];
  }

  function sort_by_position_desc( $a, $b ) {
    if ( ! (isset($a['metadata']['position']) && isset($b['metadata']['position']) ) ) {
      return 0;
    }
    return $a['metadata']['position'] < $b['metadata']['position'];
  }

  function sort_by_position_asc( $a, $b ) {
    if ( ! (isset($a['metadata']['position']) && isset($b['metadata']['position']) ) ) {
      return 0;
    }
    return $a['metadata']['position'] > $b['metadata']['position'];
  }

  /**
   * Clear cache
   *

M template/home.php => template/home.php +47 -47
@@ 1,54 1,54 @@
<!DOCTYPE html>
<html>
<head>
    <link rel="icon" href="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title>{title}</title>
    <style> :root { --b: #000; --g: #888; --w: #fff}body { font: 400 18px/1.6 sans-serif; padding: 2rem; background: var(--w); color: var(--b)}.container { max-width: 70ch }.mbh { margin: 0 0 .5rem }.mb1 { margin: 0 0 1rem }.mb2 { margin: 0 0 2rem }.g, pre { color: var(--g) }img { width: 100%; image-rendering: -moz-crisp-edges; image-rendering: pixelated }a, a:visited { color: var(--b); text-decoration-color: var(--g) }a:hover { text-decoration-color: var(--b) }pre, ul, ol, blockquote { margin: 0; padding: 0 1rem }pre { white-space: pre-wrap }@media (prefers-color-scheme: dark) { :root { --b: #fff; --g: #888; --w: #000 }} </style>
	<link rel="icon" href="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta charset="UTF-8">
	<title>{title}</title>
	<style> :root { --b: #000; --g: #888; --w: #fff}body { font: 400 18px/1.6 sans-serif; padding: 2rem; background: var(--w); color: var(--b)}.container { max-width: 70ch }.mbh { margin: 0 0 .5rem }.mb1 { margin: 0 0 1rem }.mb2 { margin: 0 0 2rem }.g, pre { color: var(--g) }img { width: 100%; image-rendering: -moz-crisp-edges; image-rendering: pixelated }a, a:visited { color: var(--b); text-decoration-color: var(--g) }a:hover { text-decoration-color: var(--b) }pre, ul, ol, blockquote { margin: 0; padding: 0 1rem }pre { white-space: pre-wrap }@media (prefers-color-scheme: dark) { :root { --b: #fff; --g: #888; --w: #000 }} </style>
</head>
<body>
<div class="container">

<div class="head mb2">
    <?php echo '<a href="'.$this->get_config()->host.'" rel="canonical">'.$this->get_config()->site_name.'</a>';
    echo ( $this->where === 'page' )? ' / {title}' : ''; ?>
</div>
    <?php if ( $this->where === 'home' ): ?>
    <div class="content">
    {content}
    </div>
    <div class="entries mb2">
        <?php
        $page_list = $this->get_list('by_date','desc');
        $pages = $this->filter( $page_list, 'flags', 'guide', true );
        foreach ( $pages as $page ) {
            echo '<div class="entry mb1">';
            echo '<a href="' . $page['url'] . '">' . $page['metadata']['title'] . '</a>' . (( $page['metadata']['date'] )? ' &mdash; ' . $page['metadata']['date'] : '');
            echo '</div>';
        }
        ?>
    </div>
    <div class="entries mb2">
    <h2>Guides</h2>
    <?php
        $guides = $this->filter( $page_list, 'flags', 'guide', false );
        foreach ( $guides as $page ) {
            echo '<div class="entry mb1">';
            echo '<a href="' . $page['url'] . '">' . $page['metadata']['title'] . '</a>' . (( $page['metadata']['date'] )? ' &mdash; ' . $page['metadata']['date'] : '');
            echo '</div>';
        }
        ?>
    </div>
    <?php endif; ?>
    <?php if ( $this->where === 'page' ): ?>
    <div class="container mb2">
        <?php echo ( $this->where === 'page' )? '{description}' : ''; ?>
        {content}
    </div>
    <?php endif; ?>
    <div class="footer">
        <img style="width:32px;height:auto;" src="<?php echo $this->get_config()->logo; ?>">
    </div></div>
	<div class="container">
		<div class="head mb2">
			<?php echo '<a href="' . $this->get_config()->host . '" rel="canonical">' . $this->get_config()->site_name . '</a>';
			echo ( $this->where === 'page' ) ? ' / {title}' : ''; ?>
		</div>
		<?php if ( $this->where === 'home' ): ?>
		<div class="content">
		{content}
		</div>
		<div class="entries mb2">
			<?php
			$page_list = $this->get_list( 'by_date','desc' );
			$pages     = $this->filter( $page_list, 'flags', 'guide', true );
			foreach ( $pages as $page ) {
				echo '<div class="entry mb1">';
				echo '<a href="' . $page['url'] . '">' . $page['metadata']['title'] . '</a>' . ( ( $page['metadata']['date'] ) ? ' &mdash;<span class="g"> ' . $page['metadata']['date'] : '' ) . '</span>';
				echo '</div>';
			}
			?>
		</div>
		<div class="entries mb2">
			<h2>Guides</h2>
			<?php
				$guides = $this->filter( $page_list, 'flags', 'guide', false );
				foreach ( $guides as $page ) {
					echo '<div class="entry mb1">';
					echo '<a href="' . $page['url'] . '">' . $page['metadata']['title'] . '</a>' . ( ( $page['metadata']['date'] ) ? ' &mdash; ' . $page['metadata']['date'] : '' );
					echo '</div>';
				}
				?>
		</div>
		<?php endif; ?>
		<?php if ( $this->where === 'page' ): ?>
		<div class="container mb2">
			<?php echo ( $this->where === 'page' ) ? '{description}' : ''; ?>
			{content}
		</div>
		<?php endif; ?>
		<div class="footer">
			<img style="width:32px;height:auto;" src="<?php echo $this->get_config()->logo; ?>">
		</div>
	</div>
</body>
</html>