tldr ¶
TLDR Pages Client Wrapper.
A pure Python implementation of the TLDR client specification v2.3, providing command documentation lookup with proper caching, localization, and platform support. This wrapper contains no Discord dependencies and can be used independently.
Classes:
-
TldrClient–Core TLDR client functionality for fetching and managing pages.
Classes¶
TldrClient ¶
Core TLDR client functionality for fetching and managing pages.
Implements the TLDR client specification v2.3 with proper caching, platform detection, and language fallback mechanisms.
Methods:
-
normalize_page_name–Normalize command name according to TLDR specification.
-
get_cache_file_path–Generate the file system path for a cached TLDR page.
-
have_recent_cache–Check if a recent cached version of a page exists.
-
load_page_from_cache–Load a TLDR page from local cache.
-
store_page_to_cache–Store a TLDR page to local cache.
-
detect_platform–Detect the default platform for Discord bot context.
-
get_language_priority–Get prioritized list of languages for Discord bot context.
-
get_platform_priority–Determine platform search order based on user input and TLDR spec.
-
fetch_tldr_page–Fetch a TLDR page with platform priority and language fallback.
-
list_tldr_commands–List available TLDR commands for a given language and platform filter.
-
parse_placeholders–Parse and format placeholder text in TLDR pages.
-
format_tldr_for_discord–Format a TLDR markdown page for Discord output.
-
not_found_message–Generate a message for when a page is not found.
-
update_tldr_cache–Update the TLDR cache for a specific language.
-
cache_needs_update–Check if the cache needs updating based on age.
-
split_long_text–Split long text into pages for Discord embeds.
Functions¶
normalize_page_name staticmethod ¶
Normalize command name according to TLDR specification.
Parameters:
-
name(str) –Raw command name that may contain spaces or mixed case.
Returns:
-
str–Normalized command name: lowercase, dash-separated, trimmed.
Examples:
>>> TldrClient.normalize_page_name("git status")
"git-status"
>>> TldrClient.normalize_page_name("GyE D3")
"gye-d3"
get_cache_file_path staticmethod ¶
have_recent_cache staticmethod ¶
load_page_from_cache staticmethod ¶
store_page_to_cache staticmethod ¶
detect_platform staticmethod ¶
detect_platform() -> str
Detect the default platform for Discord bot context.
Returns:
-
str–Platform identifier, defaults to 'linux' for container environments.
get_language_priority staticmethod ¶
get_platform_priority staticmethod ¶
Determine platform search order based on user input and TLDR spec.
Parameters:
-
user_platform_input(str | None, default:None) –User-specified platform preference.
Returns:
Notes
Implementation follows TLDR spec v2.3: - If user specifies "common", only return "common" - Otherwise: [user_platform, detected_platform, common, all_other_platforms]
fetch_tldr_page staticmethod ¶
fetch_tldr_page(
command: str, languages: list[str], platform_preference: str | None = None
) -> tuple[str, str] | None
Fetch a TLDR page with platform priority and language fallback.
Parameters:
-
command(str) –Normalized command name to fetch.
-
languages(list[str]) –Ordered list of languages to try.
-
platform_preference(str | None, default:None) –User's platform preference.
Returns:
Notes
Follows TLDR spec priority: platform takes precedence over language. Tries cache first, then remote fetch with automatic caching.
list_tldr_commands staticmethod ¶
list_tldr_commands(
language: str = "en", platform_filter: str | None = "linux"
) -> list[str]
parse_placeholders staticmethod ¶
parse_placeholders(
line: str,
show_short: bool = False,
show_long: bool = True,
show_both: bool = False,
highlight: bool = True,
) -> str
Parse and format placeholder text in TLDR pages.
Parameters:
-
line(str) –Line containing TLDR placeholder syntax.
-
show_short(bool, default:False) –Show only short options for placeholders.
-
show_long(bool, default:True) –Show only long options for placeholders.
-
show_both(bool, default:False) –Show both short and long options.
-
highlight(bool, default:True) –Whether to apply highlighting markup.
Returns:
-
str–Processed line with placeholders resolved.
_process_description_lines staticmethod ¶
_process_command_examples staticmethod ¶
format_tldr_for_discord staticmethod ¶
format_tldr_for_discord(
md: str, show_short: bool = False, show_long: bool = True, show_both: bool = False
) -> str
Format a TLDR markdown page for Discord output.
Parameters:
-
md(str) –Raw TLDR markdown content.
-
show_short(bool, default:False) –Show only short options for placeholders.
-
show_long(bool, default:True) –Show only long options for placeholders.
-
show_both(bool, default:False) –Show both short and long options.
Returns:
-
str–Formatted content suitable for Discord display.
not_found_message staticmethod ¶
update_tldr_cache staticmethod ¶
Update the TLDR cache for a specific language.
Parameters:
-
language(str, default:'en') –Language code to update cache for.
Returns:
-
str–Status message indicating success or failure.
Notes
Downloads from GitHub releases following TLDR spec v2.3. Replaces existing cache completely to ensure consistency.