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

docs: Add new doc pages #1545

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/src/Advanced/creating_custom_image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=43" -->
1 change: 1 addition & 0 deletions docs/src/Bazzite_resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=8" -->
1 change: 1 addition & 0 deletions docs/src/Gaming/Common_gaming_issues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2658" -->
1 change: 1 addition & 0 deletions docs/src/Gaming/Hardware_compatibility_for_gaming.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=35" -->
1 change: 1 addition & 0 deletions docs/src/Gaming/Managing_and_modding_games.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2657" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "htthttps://universal-blue.discourse.group/docs?topic=2743" -->
1 change: 1 addition & 0 deletions docs/src/General/Installation_Guide/secure_boot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2742" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2495" -->
1 change: 1 addition & 0 deletions docs/src/General/reporting_bugs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=3402" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2647" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2646" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2644" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun fetch_discourse_md.py "https://universal-blue.discourse.group/docs?topic=2637" -->
43 changes: 27 additions & 16 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@

</div>

# General
# Table of Contents

- [📜 Bazzite's README](Bazzite_README.md)
- [❓️ FAQ](General/FAQ.md)
- [📖 Installation Guide](General/Installation_Guide/index.md)
- [Bazzite's README](Bazzite_README.md)
- [Bazzite Resources](Bazzite_resources.md)
- [FAQ](General/FAQ.md)
- [Installation Guide](General/Installation_Guide/index.md)

- [🖥️ Installing Bazzite for Desktop/Laptop Hardware](General/Installation_Guide/Installing_Bazzite_for_Desktop_or_Laptop_Hardware.md)
- [⚙️ Installing Bazzite for Framework Laptop 16](General/Installation_Guide/Installing_Bazzite_for_Framework_Laptop_16.md)
- [💻️ Installing Bazzite for Framework Laptop 13 (AMD/Intel GPU)](General/Installation_Guide/Installing_Bazzite_Framework_Laptop_13.md)
- [🎮️ Installing Bazzite for Home Theater PC (HTPC) Setups](General/Installation_Guide/Installing_Bazzite_for_HTPC_Setups.md)\*
- [🚂 Installing Bazzite on the Steam Deck](General/Installation_Guide/Installing_Bazzite_for_Steam_Deck.md)\*
- [📟️ Installing Bazzite on Handheld PCs](General/Installation_Guide/Installing_Bazzite_for_Handheld_PCs.md)\*
- [Installing Bazzite for Desktop/Laptop Hardware](General/Installation_Guide/Installing_Bazzite_for_Desktop_or_Laptop_Hardware.md)
- [Installing Bazzite for Framework Laptop 16](General/Installation_Guide/Installing_Bazzite_for_Framework_Laptop_16.md)
- [Installing Bazzite for Framework Laptop 13 (AMD/Intel GPU)](General/Installation_Guide/Installing_Bazzite_Framework_Laptop_13.md)
- [Installing Bazzite for Home Theater PC (HTPC) Setups](General/Installation_Guide/Installing_Bazzite_for_HTPC_Setups.md)\*
- [Installing Bazzite on the Steam Deck](General/Installation_Guide/Installing_Bazzite_for_Steam_Deck.md)\*
- [Installing Bazzite on Handheld PCs](General/Installation_Guide/Installing_Bazzite_for_Handheld_PCs.md)\*
- [Installation Troubleshoot Guide](General/Installation_Guide/troubleshoot_guide.md)
- [Secure Boot Instructions](General/Installation_Guide/secure_boot.md)
- [Dual Boot Preliminary Setup and Post-Setup Guide](General/Installation_Guide/dual_boot_setup_guide.md)

\* _Indicates that Steam Gaming Mode is intended for this device and ships with the ISO._

- [📝 Desktop Environment Tweaks](General/Desktop_Environment_Tweaks.md)
- [🤝 Contributing to Bazzite](General/Contributing_to_Bazzite.md)
- [🎲 Gaming](Gaming/index.md)
- [Gaming](Gaming/index.md)
- [Game Launchers](Gaming/Game_Launchers.md)
- [Managing & Modding Games](Gaming/Managing_and_modding_games.md)
- [Common Gaming Issues](Gaming/Common_gaming_issues.md)
- [Hardware Compatibility for Gaming](Gaming/Common_gaming_issues.md)
- [Desktop Environment Tweaks](General/Desktop_Environment_Tweaks.md)
- [Reporting Bugs](General/reporting_bugs.md)

# Steam Gaming Mode / Handheld & HTPC Hardware

- [📺️ Steam Gaming Mode Overview](Handheld_and_HTPC_edition/Steam_Gaming_Mode/index.md)
- [Steam Gaming Mode Overview](Handheld_and_HTPC_edition/Steam_Gaming_Mode/index.md)
- [Change Physical Keyboard Layout for Steam Gaming Mode](Handheld_and_HTPC_edition/Change_Physical_Keyboard_Layout_for_Steam_Gaming_Mode.md)
- [Handheld Wiki](Handheld_and_HTPC_edition/Handheld_Wiki/index.md)
- [Steam Deck](Handheld_and_HTPC_edition/Handheld_Wiki/Steam_Deck.md)
Expand All @@ -46,9 +53,13 @@
- [Distrobox](Installing_and_Managing_Software/Distrobox.md)
- [Appimage](Installing_and_Managing_Software/AppImage.md)
- [rpm-ostree](Installing_and_Managing_Software/rpm-ostree.md)
- [Waydroid Setup Guide](Installing_and_Managing_Software/Waydroid_Setup_Guide.md)
- [Tutorials for Installing Other Software](Installing_and_Managing_Software/index.md)
- [Updates, Rollbacks, & Rebasing](Installing_and_Managing_Software/Updates_Rollbacks_&_Rebasing.md)
- [Updates, Rollbacks, & Rebasing](Installing_and_Managing_Software/Updates_Rollbacks_&_Rebasing/index.md)
- [Updating Guide](Installing_and_Managing_Software/Updates_Rollbacks_&_Rebasing/updating_guide.md)
- [Rolling Back System Updates](Installing_and_Managing_Software/Updates_Rollbacks_&_Rebasing/rolling_back_system_updates.md)
- [Rebase Guide](Installing_and_Managing_Software/Updates_Rollbacks_&_Rebasing/rebase_guide.md)
- [Bazzite Rollback Helper](Installing_and_Managing_Software/Updates_Rollbacks_&_Rebasing/bazzite_rollback_helper.md)
- [Waydroid Setup Guide](Installing_and_Managing_Software/Waydroid_Setup_Guide.md)

# Advanced

Expand Down
129 changes: 26 additions & 103 deletions docs/utils/fetch_discourse_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@


from argparse import ArgumentParser
from copy import copy
from datetime import datetime, UTC
import fcntl
import html
Expand All @@ -95,7 +94,7 @@
from string import Template
from sys import stdout, stderr
from time import sleep
from typing import NamedTuple
from typing import NamedTuple, cast

import requests

Expand Down Expand Up @@ -146,42 +145,19 @@
imgs_urls = re.compile(
r"<img\ssrc=\"(?P<image_cdn_url>https://(?:[a-zA-Z0-9./_-]+)).*data-base62-sha1=\"(?P<sha1>[a-zA-Z0-9]+)\".*\">"
)
hashed_images_urls = re.compile(r"upload://[a-zA-Z0-9]{27}\.(?:jpe?g|png|svg)")

@staticmethod
def is_valid_doc_topic_url(url: str) -> bool:
"""Check if the passed discourse topic url is valid doc

Args:
url (str)

Returns:
bool
"""

return (
# re.match(r"https\:\/\/universal-blue\.discourse\.group/docs\?topic=\d+", url)
re.match(
re.escape(_BASE_URL) + r"/docs\?topic=\d+",
url,
)
is not None
)
hashed_images_urls = re.compile(r"upload://([a-zA-Z0-9]{27})")

@classmethod
def transform_to_url_batch(cls, url: str) -> UrlBatch | None:
def transform_to_url_batch(cls, url: str) -> UrlBatch:
"""Input a discourse url topic and return a batch of urls such as `/raw/{id}` and `/t/{id}.json`

Args:
url (str)
"""
res = None

if not cls.is_valid_doc_topic_url(url):
raise TypeError("Url is not valid")

# Get topic id
id = re.search(re.escape(_BASE_URL) + r"/docs\?topic=(\d+)", url)
id = re.search(rf"{re.escape(_BASE_URL)}/docs\?topic=(\d+)", url)
if id is None:
raise Exception("id was not found")
id = int(id.group(1))
Expand All @@ -194,23 +170,6 @@

return res

@classmethod
def get_page_from_json(cls, batch: UrlBatch) -> HTMLPage:
"""Get webpage contents from an url link

This includes images urls from discourse cdn

Args:
batch (UrlBatch)
"""
json_content = (res := cls.fetch(batch.json_url)).json()
debug(f"{res.url} res.status = {res.status_code}")
if res.status_code != 200:
raise Exception(res.reason)

# json_content = json.loads(json_content)
return json_content["post_stream"]["posts"][0]["cooked"]

@classmethod
def fetch(cls, url: str) -> requests.Response:
tries = 2
Expand All @@ -228,41 +187,9 @@

return res

@classmethod
def get_markdown_from_raw(cls, batch: UrlBatch) -> Markdown:
"""Get markdown from page

This is recommended for extracting text transcriptions

Args:
batch (UrlBatch): _description_

Returns:
str:
"""

return requests.get(batch.raw_url).text

@classmethod
def get_images_url_assocs_from_page(cls, page: HTMLPage) -> ImageUrlAssocs:
result: list[tuple] = []
for match in re.finditer(DiscourseProcessor.Patterns.imgs_urls, page):
debug(match.__str__())
(sha1, image_cdn_url) = match.group("sha1", "image_cdn_url")
result.append((sha1, image_cdn_url))
return result

@classmethod
def replace_images_urls_in_markdown(
cls, page: Markdown, assocs: ImageUrlAssocs
) -> Markdown:
result = page
for assoc in assocs:
result = result.replace(
f"upload://{assoc[0]}",
os.path.splitext(assoc[1])[0],
)
return result
@staticmethod
def get_markdown_from_url(url: str):
return requests.get(url).text

Check warning on line 192 in docs/utils/fetch_discourse_md.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

docs/utils/fetch_discourse_md.py#L192

Requests call without timeout

@staticmethod
def add_metadata_to_markdown(md: Markdown, url_discourse: str) -> Markdown:
Expand Down Expand Up @@ -296,12 +223,18 @@
return "\n".join(md_split)


def simple_replace_match(match: re.Match) -> str:
hash = match.group(1)
if hash:
return f"{_BASE_URL}/uploads/short-url/{hash}"
return ""


def main():
argparser = ArgumentParser()
argparser.add_argument(
"url",
type=str,
nargs=1, # Change this to `+` if you wish to process multiple urls
help="discourse urls to be processed",
)
argparser.add_argument(
Expand All @@ -317,32 +250,22 @@
global _is_debug
_is_debug = os.getenv("DEBUG") == "1" or args.debug

urls = args.url

urls_batches_list: list[UrlBatch] = []
urls = cast(str, DiscourseProcessor.transform_to_url_batch(args.url).raw_url)

for url in urls:
batch = DiscourseProcessor.transform_to_url_batch(url)
if batch is None:
continue
urls_batches_list.append(batch)

for batch in urls_batches_list:
image_urls_assocs = DiscourseProcessor.get_images_url_assocs_from_page(
DiscourseProcessor.get_page_from_json(batch)
)
result = DiscourseProcessor.replace_images_urls_in_markdown(
page=DiscourseProcessor.get_markdown_from_raw(batch),
assocs=image_urls_assocs,
)
result = DiscourseProcessor.get_markdown_from_url(urls)
result = re.sub(
DiscourseProcessor.Patterns.hashed_images_urls,
simple_replace_match,
result,
)

# Remove comments
result = DiscourseProcessor.Patterns.post_sep_markdown.split(result, 1)[0].rstrip()
# Remove comments
result = DiscourseProcessor.Patterns.post_sep_markdown.split(result, 1)[0].rstrip()

# Add metadata
result = DiscourseProcessor.add_metadata_to_markdown(result, batch.source_url)
# Add metadata
result = DiscourseProcessor.add_metadata_to_markdown(result, urls)

print(result, file=stdout)
print(result, file=stdout)


if __name__ == "__main__":
Expand Down
Loading