Sorting by date
This commit is contained in:
		
							parent
							
								
									2c30a3bb30
								
							
						
					
					
						commit
						d29570739b
					
				
							
								
								
									
										38
									
								
								franca.py
								
								
								
								
							
							
						
						
									
										38
									
								
								franca.py
								
								
								
								
							| 
						 | 
					@ -12,7 +12,6 @@ import markdown2
 | 
				
			||||||
import fire
 | 
					import fire
 | 
				
			||||||
import minify_html
 | 
					import minify_html
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
from common.functions import *
 | 
					from common.functions import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,7 +20,8 @@ def load_translations(file_path, _dict):
 | 
				
			||||||
        reader = csv.DictReader(f, delimiter='|')  #
 | 
					        reader = csv.DictReader(f, delimiter='|')  #
 | 
				
			||||||
        for row in reader:  # проходим по строкам csv, каждая из которых является словарём
 | 
					        for row in reader:  # проходим по строкам csv, каждая из которых является словарём
 | 
				
			||||||
            _dict[row['id']] = row  # добавляем ключ - значение ключа id, значение - словарь row 
 | 
					            _dict[row['id']] = row  # добавляем ключ - значение ключа id, значение - словарь row 
 | 
				
			||||||
            del row['id']  # удаляем ключ по названием "id" из словаря row, так как его значение уже является ключом в словаре _dict
 | 
					            del row[
 | 
				
			||||||
 | 
					                'id']  # удаляем ключ по названием "id" из словаря row, так как его значение уже является ключом в словаре _dict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Функции, доступные в теме
 | 
					# Функции, доступные в теме
 | 
				
			||||||
| 
						 | 
					@ -29,19 +29,24 @@ def translate(id, language):
 | 
				
			||||||
    global translations
 | 
					    global translations
 | 
				
			||||||
    return translations[id][language]
 | 
					    return translations[id][language]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
translations = {}  # здесь будут переводы от темы и от сайта
 | 
					translations = {}  # здесь будут переводы от темы и от сайта
 | 
				
			||||||
config = yaml.safe_load(read_file('config.yaml'))  # Чиатем конфиг сайта
 | 
					config = yaml.safe_load(read_file('config.yaml'))  # Читаем конфиг сайта
 | 
				
			||||||
running = False  # нужно для проверки
 | 
					running = False  # нужно для проверки
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# класс для watchdog. 
 | 
					
 | 
				
			||||||
# При обнаружении изменений в папках content и themes/{config['theme']}, перегенерировать папку public
 | 
					# класс для watchdog
 | 
				
			||||||
 | 
					# Во время разработки, при обнаружении изменений в папках content и themes/{config['theme']},
 | 
				
			||||||
 | 
					# перегенерировать папку public
 | 
				
			||||||
class Develop(FileSystemEventHandler):
 | 
					class Develop(FileSystemEventHandler):
 | 
				
			||||||
    def on_modified(self, event):
 | 
					    def on_modified(self, event):
 | 
				
			||||||
        print(f'event type: {event.event_type} path : {event.src_path}')
 | 
					        print(f'event type: {event.event_type} path : {event.src_path}')
 | 
				
			||||||
        franca()
 | 
					        franca()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def on_created(self, event):
 | 
					    def on_created(self, event):
 | 
				
			||||||
        print(f'event type: {event.event_type} path : {event.src_path}')
 | 
					        print(f'event type: {event.event_type} path : {event.src_path}')
 | 
				
			||||||
        franca()
 | 
					        franca()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def on_deleted(self, event):
 | 
					    def on_deleted(self, event):
 | 
				
			||||||
        print(f'event type: {event.event_type} path : {event.src_path}')
 | 
					        print(f'event type: {event.event_type} path : {event.src_path}')
 | 
				
			||||||
        franca()
 | 
					        franca()
 | 
				
			||||||
| 
						 | 
					@ -65,10 +70,10 @@ def develop(prod):
 | 
				
			||||||
        except KeyboardInterrupt:
 | 
					        except KeyboardInterrupt:
 | 
				
			||||||
            pass
 | 
					            pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Функция для генерации сайта
 | 
					# Функция для генерации сайта
 | 
				
			||||||
def franca(prod=False):
 | 
					def franca(prod=False):
 | 
				
			||||||
    # if prod is False, then redefine config.base_url
 | 
					    if not prod:
 | 
				
			||||||
    if prod is False:
 | 
					 | 
				
			||||||
        config['base_url'] = "http://127.0.0.1:8000"
 | 
					        config['base_url'] = "http://127.0.0.1:8000"
 | 
				
			||||||
    # Load theme's jinja templates
 | 
					    # Load theme's jinja templates
 | 
				
			||||||
    templates = Environment(loader=FileSystemLoader(
 | 
					    templates = Environment(loader=FileSystemLoader(
 | 
				
			||||||
| 
						 | 
					@ -103,20 +108,20 @@ def franca(prod=False):
 | 
				
			||||||
                "content", "public").rstrip(".md")
 | 
					                "content", "public").rstrip(".md")
 | 
				
			||||||
            os.makedirs(post_path, exist_ok=True)
 | 
					            os.makedirs(post_path, exist_ok=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
            content = markdown2.markdown(post_data.content, extras=['fenced-code-blocks'])
 | 
					            content = markdown2.markdown(post_data.content, extras=['fenced-code-blocks'])
 | 
				
			||||||
            description = content.partition('<!--more-->')[0]
 | 
					            description = content.partition('<!--more-->')[0]
 | 
				
			||||||
            content = "{% import 'shortcodes.j2' as shortcodes %}" + content
 | 
					            content = "{% import 'shortcodes.j2' as shortcodes %}" + content
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            url = post_path.replace(f"public/{language}", "")
 | 
					            url = post_path.replace(f"public/{language}", "")
 | 
				
			||||||
            section = "/" if len(url.split('/')) == 2 else url.split('/')[1]
 | 
					            section = "/" if len(url.split('/')) == 2 else url.split('/')[1]
 | 
				
			||||||
 | 
					            date = post_data['date']
 | 
				
			||||||
 | 
					            posts[language].setdefault(date, {})
 | 
				
			||||||
 | 
					            posts[language][date].setdefault(section, {})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            posts[language].setdefault(section, {})
 | 
					            posts[language][date][section][url] = {
 | 
				
			||||||
 | 
					 | 
				
			||||||
            posts[language][section][url] = {
 | 
					 | 
				
			||||||
                'title': post_data['title'],
 | 
					                'title': post_data['title'],
 | 
				
			||||||
                'description': description,
 | 
					                'description': description,
 | 
				
			||||||
                'date': post_data['date'],
 | 
					                'date': date,
 | 
				
			||||||
                'content': templates.from_string(content).render()
 | 
					                'content': templates.from_string(content).render()
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -126,13 +131,15 @@ def franca(prod=False):
 | 
				
			||||||
                filename = image.split('/')[-1].split('.')[0]
 | 
					                filename = image.split('/')[-1].split('.')[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                create_thumbnail(f'assets{image}', f'public/images/{filename}_600.jpg', 600)
 | 
					                create_thumbnail(f'assets{image}', f'public/images/{filename}_600.jpg', 600)
 | 
				
			||||||
                posts[language][section][url]['image'] = f'/images/{filename}_600.jpg'          
 | 
					                posts[language][date][section][url]['image'] = f'/images/{filename}_600.jpg'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                create_thumbnail(f'assets{image}', f'public/images/{filename}_400.jpg', 400)
 | 
					                create_thumbnail(f'assets{image}', f'public/images/{filename}_400.jpg', 400)
 | 
				
			||||||
                posts[language][section][url]['thumbnail'] = f'/images/{filename}_400.jpg'
 | 
					                posts[language][date][section][url]['thumbnail'] = f'/images/{filename}_400.jpg'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        posts[language] = dict(sorted(posts[language].items(), reverse=True))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for section, urls in posts[language].items():
 | 
					        for date, sections in posts[language].items():
 | 
				
			||||||
 | 
					            for section, urls in sections.items():
 | 
				
			||||||
                if section != "/":
 | 
					                if section != "/":
 | 
				
			||||||
                    html = base.render(config=config, section=section,
 | 
					                    html = base.render(config=config, section=section,
 | 
				
			||||||
                                       language=language, posts=posts)
 | 
					                                       language=language, posts=posts)
 | 
				
			||||||
| 
						 | 
					@ -178,6 +185,7 @@ def franca(prod=False):
 | 
				
			||||||
            robots_content += "\nDisallow: /"
 | 
					            robots_content += "\nDisallow: /"
 | 
				
			||||||
        write_file('public/robots.txt', robots_content)
 | 
					        write_file('public/robots.txt', robots_content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if prod:
 | 
				
			||||||
        if 'pagefind' in config:
 | 
					        if 'pagefind' in config:
 | 
				
			||||||
            os.system("npx pagefind --source public")  # build search index
 | 
					            os.system("npx pagefind --source public")  # build search index
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue