picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.83k stars 614 forks source link

Tags and categories not fully working #496

Closed paulomorais1981 closed 5 years ago

paulomorais1981 commented 5 years ago

Hi everyone,

I'm using Pico 2.0 and trying to use tags in my pico-based blog with this plugin.

To create categories, i've made a tree just like this:

content
├- category-one
│ ├ post-one.md
│ ├ post-two.md
│ ├ index.md
└ - category-two
│ ├ post-three.md
│ ├ post-four.md
│ └ index.md

All the files have tags meta, but not all tags I've wrote appears on my home.

The tags and categories section is like this:

<section class="section">
  <div class="columns">
    <div class="column is-offset"></div>
    <div class=column>
      <p class="title is-4">Tags</p>
      <div class="tags">
        {% for tag in tag_list %}
        <div class="tag is-white is-medium"><a href="{{ base_url }}/tag/{{ tag|e('url') }}">{{ tag|e }}</a></div>
        {% endfor %}
      </div>
    </div>
    <div class=column>
      <p class="title is-4">Categorias</p>
      <div class="tags">
        {% for category in pages["index"].tree_node.children %}
        {% if category.page and category.page.meta.category %}
        <div class="tag is-white is-medium"><a href="{{ category.page.url }}">{{ category.page.title }}</a></div>
        {% endif %}
        {% endfor %}
      </div>
    </div>
    <div class="column is-offset"></div>
  </div>

</section>

Anyway, all the settings are just like the readmes told me to do, but not all tags appeared at homepage and no categories are showed. And if i type localhost/tag/something it gives me a 404 error page.

Is this something to do with AllowOverride All or .htaccess things? Or is something else?

PhrozenByte commented 5 years ago

According to the README.md, there's a delunique option:

Do you want to remove from the tags list the ones that are used in only one page ?

Check whether this option is removing some of your tags. What 404 error page is shown? Pico's 404 page, or from your webserver (Apache, nginx, Lighttpd, ...)?

Can you please provide the contents of content/category-one/index.md and content/category-one/post-one.md?

paulomorais1981 commented 5 years ago

About tags, ok, my fault, I reinstalled the plugin and did not checked it. Sorry.

This are the contents of the files of index.md

---
title: Ferroviários
category: true
template: category
Hidden: 1
---

And of post-one.md

---
projeto: Memória dos Ferroviários 
Author: Paulo Morais e Andressa Gonçalves
Title: No começo foi catando lenha
Date: 2018-04-21
Template: post
Tags: ferroviários,Suíça,Três Corações,Brasil,Ademar
Depoimento: 1
Description: Meu nome é Ademar Magalhães Neves, nasci em  23 de novembro de 1923, estou com 88 anos! Eu nasci em Barra Mansa, mas vim pra cá com a idade de 7 anos. Eu cheguei criança e entrei logo no grupo escolar Bueno Brandão, ali eu fiz o até  quarto ano só, não tinha recurso de estudar.
Capa: 01.png
---

**Nome completo e data de nascimento?**

Meu nome é Ademar Magalhães Neves, nasci em  23 de novembro de 1923, estou com 88 anos! Eu nasci em Barra Mansa, mas vim pra cá com a idade de 7 anos. Eu cheguei criança e entrei logo no grupo escolar Bueno Brandão, ali eu fiz o até  quarto ano só, não tinha recurso de estudar. 

Entrei na rede catando lenha, no começo foi catando lenha, os fazendeiros cortavam a lenha e punham na estrada e vendiam para a ferrovia, e nós era uma turma de trabalhava no lastro. Saiam seis na máquina a vapor chegava no local, nas fazendas, e carregava a lenha na beira da linha. Hoje nem cortar uma árvore a gente não pode. Naquele tempo chegava a fazer estoque de 100 mil metros de lenha para as locomotivas a vapor.  Depois de trabalhar dois anos neste lastro particular e que saí efetivo na rede e entrei direto no torno, não sabia nada, mas o chefe mandou, e de fato que peguei logo, o pessoal admirava a forma como eu trabalhava lá, fazia render o serviço. O torno fazia tudo quanto era peça. Parafuso, rosca, pinos. Torneava aro de roda, roda,  até isto torneava. 
PhrozenByte commented 5 years ago

Tested your code, works just fine for me:

Bildschirmfoto von 2019-05-15 14-14-13

        <h2>Categories</h2>
        <ul>
            {% for category in pages["index"].tree_node.children %}
                {% if category.page and category.page.meta.category %}
                    <li><a href="{{ category.page.url }}">{{ category.page.title }}</a></li>
               {% endif %}
            {% endfor %}
        </ul>
paulomorais1981 commented 5 years ago

Well, I've pasted your code, unchanged, and it did not worked for me, it gave no category.

image

I will have to study it and see what's going on.

paulomorais1981 commented 5 years ago

Now i've passed the day thinking about a solution and I think I found it. But I am a very weak amateur programmer and I think this code have to be checked, but I believe it worked.

First of all, I added in all my posts a meta categoria, e. g. categoria: documents.

For each category I want to create, i created a file, like documents.md in the example above, with this yaml header:

---
Title: Documents
nome: documents
Template: category
Hidden: 1
---

The category.twig has this code:

            <div class="content">
                {% for page in pages|sort_by("time")|reverse %}
                {% if page.meta.categoria == meta.nome %}
                <h3 class="title is-3"><a href="{{ page.url }}">{{ page.title }}</a></h3>
                <div class="columns">
                    <div class="column is-3">
                        {% if page.meta.capa %}
                        <img src="{{site_url}}/assets/{{ page.meta.capa }}">
                        {% endif %}
                    </div>
                    <div class="column is-9">
                        <div class="content">
                            <p class="date">{{ page.date_formatted }}</p>
                            <p class="excerpt">{{ page.description }}</p>
                        </div>
                    </div>
                </div>
                <hr>
                {% endif %}
                {% endfor %}
            </div>

And at the end of my index.twig i put this code:

  <script type="text/javascript">
    var myArr = [];
  </script>
  {% for page in pages|sort_by("time")|reverse %}
  {% if page.meta.categoria %}
  <script type="text/javascript">
    var adicionar = myArr.push('{{ page.meta.categoria }}');
  </script>
  {% endif %}
  {% endfor %}
  <script type="text/javascript">
    Array.prototype.unique = function (a) {
      return function () {
        return this.filter(a)
      }
    }(function (a, b, c) {
      return c.indexOf(a, b + 1) < 0
    });
    var cats = (myArr.unique());
var div1 = '<div class="tag is-medium is-white"><a href="{{ site_url }}';
var div2 = '">';
var div3 = '</div>';
var categ = '';
function criar(item) {
    categ = categ + div1 + item + div2 + item + div3; 
}

cats.forEach(criar);
console.log(cats);
console.log(categ);
document.getElementById('categorias').innerHTML = categ;
  </script>

So, at the <div id="categorias"> i got my categories. And the categories pages are all working. I can put all the files at same subfolder, just categorizing it at yaml's meta, as we do with tags.

Apparently, it's working finely.

[post-edit]: i'm now thinking... if this solution above is useful, it can be used to create any taxonomy you want. Just create the files and meta and adapt javascript code in index.twig and you can create taxonomies like type [ video, audio, post, note etc] or something like it...