diff --git a/.github/workflows/update-docset.yml b/.github/workflows/update-docset.yml new file mode 100644 index 0000000..733051a --- /dev/null +++ b/.github/workflows/update-docset.yml @@ -0,0 +1,68 @@ +name: Update Docset + +on: + schedule: + - cron: '0 0 1 * *' + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Create version environment variable + run: echo "new_version=$(date -u +%Y-%m-%d)" >> $GITHUB_ENV + + + # Build docset + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set version number + run: sed -i "s/YYYY-MM-DD/$new_version/" static/Info.plist + + - name: Build docset + run: docker compose up + + + # Create PR + + - name: Sync contributions repo + run: gh repo sync cvn/Dash-User-Contributions -b master --force + env: + GITHUB_TOKEN: ${{ secrets.DASH_REPO_TOKEN }} + + - name: Checkout code + uses: actions/checkout@v4 + with: + repository: cvn/Dash-User-Contributions + token: ${{ secrets.DASH_REPO_TOKEN }} + path: Dash-User-Contributions + + - name: Update version number in docset.json + run: | + sed -i "s/\"version\": \".*\"/\"version\": \"$new_version\"/" Dash-User-Contributions/docsets/tldr/docset.json + + - name: Move docset + run: mv tldr_pages.tgz Dash-User-Contributions/docsets/tldr/ + + - name: Commit changes + run: | + cd Dash-User-Contributions + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git checkout -b tldr-update + git add --all . + git commit -m "Update TLDR pages docset to $new_version" + git push --force origin tldr-update + + - name: Create pull request to upstream + run: | + cd Dash-User-Contributions + gh pr create \ + --repo Kapeli/Dash-User-Contributions \ + --head cvn:tldr-update \ + --title "Update TLDR pages docset to $new_version" \ + --body "This is an automated update created by [a workflow](https://github.com/cvn/tldr-python-dash-docset/actions/workflows/update-docset.yml)." + env: + GITHUB_TOKEN: ${{ secrets.DASH_REPO_TOKEN }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4ffa7fe --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3 + +RUN mkdir /code +COPY requirements.txt /code/ +WORKDIR /code +RUN pip install -r requirements.txt +COPY . /code/ + +CMD python generator.py diff --git a/README.md b/README.md index cd9523b..bb22893 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,25 @@ user manual. Thanks to [tldr.jsx][4] for your great CSS! +### This is a fork + +The original author of this project, [Moddus](https://github.com/Moddus/tldr-python-dash-docset), appears to have abandoned it. This fork contains some [bug fixes and small improvements](https://github.com/Moddus/tldr-python-dash-docset/pull/5) to the original. + +### Build + +If you have python 3 and the [required packages](requirements.txt): + + python generator.py + +Or use Docker to create an environment and build: + + docker compose up + +### Updating Dash + +This repo has [a workflow](https://github.com/cvn/tldr-python-dash-docset/actions/workflows/update-docset.yml) that regularly updates the docset in Dash. + + [1]: http://kapeli.com/dash [2]: http://zealdocs.org/ [3]: https://github.com/tldr-pages/tldr diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..442f526 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: '2' + +services: + builder: + build: . + volumes: + - .:/code diff --git a/generator.py b/generator.py index d4076ad..4b057c5 100755 --- a/generator.py +++ b/generator.py @@ -1,15 +1,27 @@ #!/usr/bin/env python3 import requests as req, zipfile, io, markdown2 as md, sqlite3, os, shutil, tarfile +import re -html_tmpl = """
%content%
""" +html_tmpl = """ + + + + + +
+
{content}
+
+ +""" -doc_source = "https://github.com/tldr-pages/tldr/archive/master.zip" +online_url = "https://github.com/tldr-pages/tldr/blob/main/pages" +doc_source = "https://github.com/tldr-pages/tldr/archive/refs/heads/main.zip" docset_path = "tldrpages.docset" doc_path_contents = docset_path + "/Contents/" doc_path_resources = docset_path + "/Contents/Resources/" doc_path = docset_path + "/Contents/Resources/Documents/" -doc_pref = "tldr-master/pages" +doc_pref = "tldr-main/pages/" if os.path.exists(doc_path): try: shutil.rmtree(doc_path) @@ -36,12 +48,12 @@ cur.execute('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);') # Generate tldr pages to HTML documents -markdowner = md.Markdown() +markdowner = md.Markdown(extras=["code-friendly"]) with zipfile.ZipFile(io.BytesIO(r.content), "r") as archive: for path in archive.namelist(): if path.startswith(doc_pref) and path.endswith(".md"): cmd_name = path[path.rfind("/")+1:-3] - sub_dir = path[len(doc_pref)+1:path.rfind("/")] + sub_dir = path[len(doc_pref):path.rfind("/")] sub_path = os.path.join(doc_path, sub_dir) if not os.path.exists(sub_path): try: os.mkdir(sub_path) @@ -54,19 +66,20 @@ else: cur.execute('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)', (cmd_name, 'Command', sub_dir+'/'+cmd_name+".html")) doc = markdowner.convert(archive.read(path)) - doc = html_tmpl.replace("%content%", doc) - with open(os.path.join(doc_path, path[len(doc_pref)+1:].replace(".md", ".html")), "wb") as html: + doc = re.sub(r'{{(.*?)}}', r'\1', doc) + doc = html_tmpl.format(url=online_url+'/'+sub_dir+'/'+cmd_name+'.md', content=doc) + with open(os.path.join(doc_path, path[len(doc_pref):].replace(".md", ".html")), "wb") as html: html.write(doc.encode("utf-8")) db.commit() db.close() # Generate tldr pages index.html with open(os.path.join(doc_path, "index.html"), "w+") as html: - html.write('

TLDR pages Docset


powered by tldr-pages.github.io/') - for dir in os.listdir(doc_path): + html.write('TLDR pages

TLDR pages


powered by tldr-pages.github.io/') + for dir in sorted(os.listdir(doc_path)): if os.path.isdir(os.path.join(doc_path, dir)): - html.write("

%s