Skip to content
Snippets Groups Projects
Commit 3ac12303 authored by Štěpán Farka's avatar Štěpán Farka Committed by jan.bednarik
Browse files

[FIX] markdown image, prague specifics

parent 4b496099
Branches
No related tags found
2 merge requests!442Release,!432Feature/majak imports
...@@ -6,7 +6,6 @@ import zipfile ...@@ -6,7 +6,6 @@ import zipfile
from datetime import date from datetime import date
from http.client import InvalidURL from http.client import InvalidURL
from io import StringIO from io import StringIO
from shutil import rmtree
from sys import stdout from sys import stdout
from typing import List from typing import List
from urllib.error import HTTPError from urllib.error import HTTPError
...@@ -15,6 +14,7 @@ import markdown.serializers ...@@ -15,6 +14,7 @@ import markdown.serializers
import yaml import yaml
from django.contrib.messages import ERROR, INFO, SUCCESS, WARNING from django.contrib.messages import ERROR, INFO, SUCCESS, WARNING
from django.core.files.images import ImageFile from django.core.files.images import ImageFile
from django.utils import timezone
from markdown import Markdown from markdown import Markdown
from markdown.extensions import Extension from markdown.extensions import Extension
from markdown.inlinepatterns import InlineProcessor from markdown.inlinepatterns import InlineProcessor
...@@ -59,8 +59,9 @@ class ImgProcessor(InlineProcessor): ...@@ -59,8 +59,9 @@ class ImgProcessor(InlineProcessor):
el.attrib["format"] = "left" el.attrib["format"] = "left"
collection = get_collection() collection = get_collection()
# TODO FIX REPO_NAME # TODO FIX REPO_NAME
parsed_image_path = get_parsed_file_path(m.group(2))
image_obj = get_or_create_image( image_obj = get_or_create_image(
params["path"], m.group(2), collection=collection, repo_name="" params["path"], parsed_image_path, collection=collection, repo_name=""
) )
if not image_obj: if not image_obj:
message_list.append( message_list.append(
...@@ -75,6 +76,17 @@ class ImgProcessor(InlineProcessor): ...@@ -75,6 +76,17 @@ class ImgProcessor(InlineProcessor):
return el, m.start(0), m.end(0) return el, m.start(0), m.end(0)
def get_parsed_file_path(path: str):
if "{{" in path:
try:
parsed_path = path.split("{{")[1].split("|")[0].split("'")[1]
except IndexError:
parsed_path = path.split("{{")[1].split("|")[0].split('"')[1]
return parsed_path
else:
return path
class ImgExtension(Extension): class ImgExtension(Extension):
def extendMarkdown(self, md): def extendMarkdown(self, md):
IMG_PATTERN = r"!\[(.*?)\]\((.*?)\)" IMG_PATTERN = r"!\[(.*?)\]\((.*?)\)"
...@@ -133,11 +145,14 @@ def import_post(path, file_path, parent, title_suffix, dry_run, repo_name): ...@@ -133,11 +145,14 @@ def import_post(path, file_path, parent, title_suffix, dry_run, repo_name):
article.content = [("text", RichText(html))] article.content = [("text", RichText(html))]
article.perex = get_perex(md) article.perex = get_perex(md)
if meta.get("date", None):
meta_date = meta["date"] meta_date = meta["date"]
if isinstance(meta_date, date): if isinstance(meta_date, date):
article.date = meta_date article.date = meta_date
else: else:
article.date = meta["date"].split()[0] article.date = meta["date"].split()[0]
else:
article.date = timezone.now()
article.title = meta["title"] article.title = meta["title"]
article.author = meta.get("author", "Česká pirátská strana") article.author = meta.get("author", "Česká pirátská strana")
...@@ -196,6 +211,15 @@ def get_or_create_image(path, file_path, collection, repo_name) -> Image or None ...@@ -196,6 +211,15 @@ def get_or_create_image(path, file_path, collection, repo_name) -> Image or None
image = Image(title=file_path, file=file, collection=collection) image = Image(title=file_path, file=file, collection=collection)
image.save() image.save()
return image return image
except FileNotFoundError:
try:
file = ImageFile(
open(os.path.join(path, "assets/img", file_path), "rb"),
name=file_path,
)
image = Image(title=file_path, file=file, collection=collection)
image.save()
return image
except FileNotFoundError: except FileNotFoundError:
img_name = file_path.split("/")[-1] img_name = file_path.split("/")[-1]
img_assets_folder = repo_name.split(".")[0] # TODO make as form field img_assets_folder = repo_name.split(".")[0] # TODO make as form field
...@@ -239,10 +263,12 @@ def clone_repo(url: str) -> (str, str): ...@@ -239,10 +263,12 @@ def clone_repo(url: str) -> (str, str):
repo_name = url.split("/")[-1] repo_name = url.split("/")[-1]
repo_path = os.path.join(path, repo_name) repo_path = os.path.join(path, repo_name)
os.chdir(path)
if os.path.exists(repo_path): if os.path.exists(repo_path):
rmtree(repo_path) os.chdir(repo_path)
os.system("git pull --depth 1")
return repo_path, repo_name
os.chdir(path)
os.system("git clone --depth 1 {}".format(url)) os.system("git clone --depth 1 {}".format(url))
return repo_path, repo_name return repo_path, repo_name
...@@ -369,6 +395,14 @@ def article_parser( ...@@ -369,6 +395,14 @@ def article_parser(
defaults={"is_permanent": True, "redirect_page": article}, defaults={"is_permanent": True, "redirect_page": article},
) )
# TODO handle redirects! PRAGUE X CB
# Redirect.objects.get_or_create(
# site=site,
# old_path="/%s/%s.html"
# % (articlepath, slug),
# defaults={"is_permanent": True, "redirect_page": article},
# )
success_counter += 1 success_counter += 1
else: else:
msg = "ERROR: Nepodporovaná přípona souboru: %s" % ext msg = "ERROR: Nepodporovaná přípona souboru: %s" % ext
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment