1. Purpose

Suppose, using the Pelican static site generator, I write an article (to be exact, page ; in Pelican, this is not exactly what the article , but still the term статья rather than the страница seems to me more appropriate) in Markdown. The article file is located along the path /pages/myfolder/myarticle.md . It is necessary that after compiling Markdown in HTML, the paths along which the files are located — in this case myfolder — would not be myfolder , and the output would look like this: myfolder/myarticle.html .


2. Desired behavior

The structure of my content folder, where I write articles in Markdown:

 └───pages ├───Life-hacks │ Neobyknovennaya.md │ Sasha-Idealna.md │ └───Sublime Text Sasha-Bozhestvenna.md 

The structure of my output folder, where articles written in Markdown are compiled.

 └───pages ├───Life-hacks │ Neobyknovennaya.html │ Sasha-Idealna.html │ └───Sublime Text Sasha-Bozhestvenna.html 

3. Actual behavior

Now the output folder looks like this:

 ├───pages │ Neobyknovennaya.html │ Sasha-Bozhestvenna.html │ Sasha-Idealna.html 

Folders Life-hacks and Sublime Text disappeared during assembly.


4. my settings

Part of my publishconf.py file:

 USE_FOLDER_AS_CATEGORY = True PAGE_URL = '{slug}.html' PAGE_SAVE_AS = PAGE_URL CATEGORY_URL = '{slug}/index.html' CATEGORY_SAVE_AS = CATEGORY_URL 

5. Steps to play

I installed the pelican and markdown packages via pip → I launch the terminal, being in the root folder of the test site, → I enter the pelican content command → I get the actual behavior, not the desired behavior.


6. Did not help

  1. In the URL Settings section of the Pelican documentation, where it describes how the site URL will look like, I did not find any variable that includes path names.
  2. Googled the answer to a similar question , but the configuration proposed by the author of the answer did not help. I get the same behavior.
  3. I looked through the list of sites made on Pelican , but I couldn’t find one with the folder structure I needed.

7. Do not offer

  1. Please do not give unargumented comments of the form юзай $другой генератор сайтов$ .
  2. Yes, I know that you can put tags and categories on articles in Pelican, but it is necessary that the folder names are also contained in the URL.

8. Software and hardware environment

Operating system:
Windows 10 Enterprise LTSB 64-bit EN
Python:
3.6.0
Pelican:
3.7.1

  • And I did not understand something, why is the page, and not the article? - andreymal
  • @andreymal, as I understand it from the documentation , articles in contrast to pages , are related to the date. I'm not going to use the date in the URL, sort by date, write news articles on the site - Sasha Chernykh
  • or other temporarily relevant materials , etc. Thank you. - Sasha Chernykh
  • Personally, I just put down the date by default and everywhere I ignore it and not worried) - andreymal

2 answers 2

I rummaged through the Pelican source code and found two interesting things override_url Page override_url and override_save_as - by redefining them, you can write files anywhere. After this, such a plugin was born (there are documentation about their writing):

The file with the settings pelicanconf.py

 PLUGINS = ['pagefixer'] 

The pagefixer.py plugin itself

 #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals from itertools import chain from pelican import signals def fix_pages(generator): for page in chain(generator.translations, generator.pages, generator.hidden_translations, generator.hidden_pages): # Получаем путь исходника относительно каталога content new_path = page.get_relative_source_path() # Заменяем расширение на html new_path = new_path.rsplit('.', 1)[0] + '.html' # И пихаем в страницу вместо родных путей относительно output page.override_url = new_path page.override_save_as = new_path def register(): signals.page_generator_finalized.connect(fix_pages) 

And in this way we get the paths we need.

Internal links of the form [текст]({filename}../pages/foo/bar.md) also work correctly and take into account any changes in the override_url .

Ps1. Never wrote plugins to Pelican before, so constructive sneakers are accepted

PS2. I didn’t check how it works on Windows, maybe backslashes are used there, in which case you will need to assign import os; new_path = new_path.replace(os.path.sep, '/') import os; new_path = new_path.replace(os.path.sep, '/') or something like that

  • Wow! And it works in Windows. - Sasha Chernykh
  • Andreymal, all the same, relative links through {filename} can be created only on files from the content folder. Is it possible to make relative links to files from other folders of the site directory, for example, themes ? Here the details of this question, which I recently asked. Thank. - Sasha Chernykh
  • one
    It seems that the need for links beyond the content indicates a jamb in architecture and it’s better to get rid of this need, the content should not be tied to a specific topic - andreymal
  • andreymal, then how best to proceed? Transfer images, css, scripts to content ? But then such a problem: these files will be duplicated in output . Since there are no variables in them, I don’t understand why there are so many duplicates that will only interfere. Thank. - Sasha Chernykh
  • It depends on the goal, which I did not quite understand at a quick run through the sources of the site. Although I suspect that there will be blocks-templates like any Wikipedia here, but I didn’t find how to do them in Pelican, so now I’m probably no longer an assistant - andreymal

1. Solution

The problem is resolved.

You must specify the folder in the variable Slug Markdown-page. For example, in the metadata of the SashaIdealna.md file, the line Slug: Life-hacks\Sasha-IdealnaSasha-Idealna.html will be placed in the Life-hacks folder and after compilation.


2. The disadvantage

We'll have to specify the folder in the metadata of each article / page that was written - that time does not save. If someone knows a more convenient way, when you need to change something in the configuration once, unsubscribe, your answer will be accepted.