Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCK-4] Add plain text files to build process of artifacts #56

Merged
merged 16 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cc_licenses/templates/includes/legalcode_40_license.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load bidi i18n license_tags %}
<div id="legal-code-body" class="padding-larger margin-top-bigger has-text-black">
{% include 'includes/snippet/icon_header_snippet.html' %}
<div>
<div id="plain-text-marker"> {# see publish management command #}
<h3 class="padding-bottom-normal b-header">
{% if legalcode|is_one_of:"by" %}{# Long full title #}
{% blocktrans trimmed %}Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License{% endblocktrans %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% load i18n %}

<div id="legal-code-plain-text" style="font-weight: bold;" class="padding-vertical-normal" >
<p class="body-big"><a href="#" class="link">{% trans "View Legal Code as plain text" %}</a></p>
<p class="body-big"><a href="{{ request.get_full_path }}.txt" class="link" download="{{ legalcode.fat_code }}.txt">{% trans "View Legal Code as plain text" %}</a></p>
Audiosutras marked this conversation as resolved.
Show resolved Hide resolved
</div>
34 changes: 34 additions & 0 deletions licenses/management/commands/publish.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import fnmatch
import os
import subprocess
import tempfile
from argparse import ArgumentParser
from shutil import rmtree

import git
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.management import BaseCommand, CommandError
from django_distill.distill import urls_to_distill
Expand Down Expand Up @@ -57,6 +61,35 @@ def add_arguments(self, parser: ArgumentParser):
def _quiet(self, *args, **kwargs):
pass

def output_text_files(self, *args, **kwargs):
output_dir = getattr(settings, "DISTILL_DIR", None)
licenses_dir = f"{output_dir}licenses/"
pattern = "legalcode*"
for root, dirs, files in os.walk(licenses_dir):
for filename in fnmatch.filter(files, pattern):
file_path = os.path.join(root, filename)
txt_filename = f"{filename}.txt"
txt_file_path = os.path.join(root, txt_filename)
with open(file_path, "r") as f:
soup = BeautifulSoup(f, "lxml")
plain_text_soup = soup.find(id="plain-text-marker")
plain_text_html = plain_text_soup.prettify(formatter="html")
with tempfile.NamedTemporaryFile(mode="w+t") as temp:
temp.write(plain_text_html)
temp.seek(0)
Audiosutras marked this conversation as resolved.
Show resolved Hide resolved
subprocess.run(
[
"pandoc",
"-f",
"html",
temp.name,
"-t",
"plain",
"-o",
txt_file_path,
]
)

def run_django_distill(self):
"""Outputs static files into the specified directory determined by settings.base.DISTILL_DIR
"""
Expand All @@ -79,6 +112,7 @@ def publish_branch(self, branch: str):
with git.Repo(settings.TRANSLATION_REPOSITORY_DIRECTORY) as repo:
setup_local_branch(repo, branch, settings.OFFICIAL_GIT_BRANCH)
self.run_django_distill()
self.output_text_files()
if repo.is_dirty():
repo.index.add("build")
commit_and_push_changes(repo, "Updated built HTML files")
Expand Down