### Complete CHM to Markdown Conversion Pipeline Source: https://context7.com/chenchuiqing/chm-to-markdown/llms.txt A bash script demonstrating the end-to-end workflow for converting CHM files to Markdown. It includes steps for decompiling CHM to HTML, converting HTML to Markdown, and optionally converting specific subdirectories. This pipeline preserves directory structure. ```bash # Step 1: Navigate to project directory cd /path/to/device-sdk-chm-to-md # Step 2: Decompile CHM to HTML (creates out_html directory) uv run python chm_to_html.py "DeviceNetworkSDK_Manual.chm" -o out_html # Step 3: Convert all HTML to Markdown (creates out_md directory) uv run python html_to_md.py --root out_html --out-root out_md # Step 4: Convert only specific sections if needed uv run python html_to_md.py --root out_html --out-root out_md --subdir "API" uv run python html_to_md.py --root out_html --out-root out_md --subdir "Structures" # Result: Directory structure preserved # out_html/API/GetDeviceInfo.html → out_md/API/GetDeviceInfo.md # out_html/Structures/DeviceConfig.html → out_md/Structures/DeviceConfig.md ``` -------------------------------- ### Post-process Markdown Cleanup Source: https://context7.com/chenchuiqing/chm-to-markdown/llms.txt Cleans raw Markdown by replacing non-breaking spaces, removing control characters, converting .html links to .md, and normalizing excessive blank lines. It takes a string of Markdown as input and returns a cleaned string. ```python from html_to_md import postprocess_markdown raw_markdown = """ # Title Some text with\xa0non-breaking\xa0spaces. Check [this link](api.html) and [anchor](page.html#section). Too many blank lines above. """ cleaned = postprocess_markdown(raw_markdown) print(cleaned) ``` -------------------------------- ### Decompile CHM to HTML using Python Source: https://context7.com/chenchuiqing/chm-to-markdown/llms.txt This script decompiles Windows CHM files into individual HTML files. It utilizes the `hh.exe` utility and can be used programmatically or via the command line. The output is organized into a specified directory, preserving the original structure. Requires Windows and `hh.exe` to be in the system PATH. ```python # Command-line usage - Basic decompilation to default 'html' directory # uv run python chm_to_html.py "DeviceSDK_Manual.chm" # Command-line usage - Custom output directory # uv run python chm_to_html.py "DeviceSDK_Manual.chm" -o out_html # Programmatic usage from pathlib import Path from chm_to_html import decompile_chm, find_hh_exe # Check if hh.exe is available hh_path = find_hh_exe() if hh_path: print(f"Found hh.exe at: {hh_path}") # Decompile CHM file chm_file = Path("DeviceSDK_Manual.chm") output_dir = Path("extracted_html") exit_code = decompile_chm(chm_file, output_dir) if exit_code == 0: print("CHM decompilation completed successfully") else: print(f"Decompilation failed with exit code: {exit_code}") else: print("hh.exe not found - this script requires Windows") # Expected output: # Using: C:\Windows\hh.exe # CHM file: C:\path\to\DeviceSDK_Manual.chm # Output directory: C:\path\to\extracted_html # Executing command: C:\Windows\hh.exe -decompile C:\path\to\extracted_html C:\path\to\DeviceSDK_Manual.chm # CHM decompilation completed. ``` -------------------------------- ### Convert HTML to Markdown with Encoding Detection using Python Source: https://context7.com/chenchuiqing/chm-to-markdown/llms.txt This script performs batch conversion of HTML files to Markdown. It intelligently detects and handles various Chinese character encodings (UTF-8, GBK, GB2312, GB18030), cleans up scripts and styles, and transforms links. It can process all HTML files in a directory, a specific subdirectory, or convert a single HTML string. Output can be directed to a separate directory. ```python # Command-line usage - Convert all HTML files from default 'html' directory # uv run python html_to_md.py # Command-line usage - Specify HTML root directory # uv run python html_to_md.py --root out_html # Command-line usage - Convert only a specific subdirectory # uv run python html_to_md.py --root out_html --subdir "Structures" # Command-line usage - Output Markdown to separate directory # uv run python html_to_md.py --root out_html --out-root out_md # Programmatic usage from pathlib import Path from html_to_md import ( html_to_markdown, detect_and_read, collect_html_files, process_file ) # Convert a single HTML string to Markdown html_content = """

API Reference

This is the main documentation.

See details """ markdown_output = html_to_markdown(html_content) print(markdown_output) # Output: # # API Reference # # This is the **main** documentation. # # [See details](details.md) # Read file with automatic encoding detection html_root = Path("out_html") md_root = Path("out_md") # Collect all HTML files in a directory html_files = collect_html_files(html_root) print(f"Found {len(html_files)} HTML files to convert") # Process each file for html_file in html_files: process_file(html_file, html_root, md_root) # Converts: out_html/Structures/DeviceInfo.html # To: out_md/Structures/DeviceInfo.md ``` -------------------------------- ### Detect Encoding and Read File Content using Python Source: https://context7.com/chenchuiqing/chm-to-markdown/llms.txt This function reads HTML files by automatically detecting their character encoding. It prioritizes common Chinese encodings like UTF-8, GBK, GB2312, and GB18030, falling back to UTF-8 with error ignoring if necessary. This ensures accurate reading of content with diverse character sets. ```python from pathlib import Path from html_to_md import detect_and_read # Read a file with unknown encoding file_path = Path("out_html/guide/introduction.html") try: content = detect_and_read(file_path) print(f"Successfully read {len(content)} characters") print(content[:200]) # Preview first 200 chars except RuntimeError as e: print(f"Failed to decode file: {e}") # The function tries encodings in order: # 1. UTF-8 (strict) # 2. GBK (strict) # 3. GB2312 (strict) # 4. GB18030 (strict) # 5. UTF-8 (ignore errors) - fallback ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.