Compare commits

...
Author SHA1 Message Date
Jared Dillard 935a964c7e bump version to 0.2.3 2025-05-20 01:20:36 -07:00
Jared Dillard 51f6c71de3 update changelog 2025-05-20 01:18:44 -07:00
Jared DillardandGitHub 70defd3996 Remove get_and_resolve_toctree method (#19) 2025-05-20 01:08:40 -07:00
Jared DillardandGitHub 9ae05c6c13 Simplify _sources lookup (#18) 2025-05-20 00:50:06 -07:00
Jared Dillard 5581979cac make a href 2025-05-18 21:24:14 -07:00
Jared Dillard f70f1a26ec add soft transfer 2025-05-18 21:21:34 -07:00
Jared DillardandGitHub ed50138ae4 Update README.md 2025-05-18 21:12:16 -07:00
Jared DillardandGitHub 8f4d2c07c6 Update docs and README (#17)
* Move readme content to index.rst

* Clean up project name

* Add advanced configuration
2025-05-18 21:10:49 -07:00
10 changed files with 306 additions and 203 deletions
+10
View File
@@ -1,6 +1,16 @@
Changelog Changelog
========= =========
0.2.3
-----
- Remove ``get_and_resolve_toctree`` method
`#19 <https://github.com/jdillard/sphinx-llms-txt/pull/19>`_
- Simplify ``_sources`` lookup
`#18 <https://github.com/jdillard/sphinx-llms-txt/pull/18>`_
- Add sphinx docs
`#16 <https://github.com/jdillard/sphinx-llms-txt/pull/16>`_
0.2.2 0.2.2
----- -----
+3 -81
View File
@@ -1,91 +1,13 @@
# Sphinx llms.txt generator # Sphinx llms.txt generator
A Sphinx extension that generates a summary `llms.txt` file, written in Markdown, and a single combined documentation `llms-full.txt` file, written in reStructuredText. A Sphinx extension that generates a summary `llms.txt` file and a single combined documentation `llms-full.txt` file.
[![PyPI version](https://img.shields.io/pypi/v/sphinx-llms-txt.svg)](https://pypi.python.org/pypi/sphinx-llms-txt) [![PyPI version](https://img.shields.io/pypi/v/sphinx-llms-txt.svg)](https://pypi.python.org/pypi/sphinx-llms-txt)
[![Downloads](https://static.pepy.tech/badge/sphinx-llms-txt/month)](https://pepy.tech/project/sphinx-llms-txt) [![Downloads](https://static.pepy.tech/badge/sphinx-llms-txt/month)](https://pepy.tech/project/sphinx-llms-txt)
## Installation ## Documentation
```bash See [sphinx-llms-txt documentation](https://sphinx-llms-txt.readthedocs.io/en/latest/index.html) for installation and configuration instructions.
pip install sphinx-llms-txt
```
## Usage
1. Add the extension to your Sphinx configuration (`conf.py`):
```python
extensions = [
'sphinx_llms_txt',
]
```
## Configuration Options
### `llms_txt_full_file`
- **Type**: boolean
- **Default**: `'True'`
- **Description**: Whether to write the single output file
### `llms_txt_full_filename`
- **Type**: string
- **Default**: `'llms-full.txt'`
- **Description**: Name of the single output file
### `llms_txt_full_max_size`
- **Type**: integer or `None`
- **Default**: `None` (no limit)
- **Description**: Sets a maximum line count for `llms_txt_full_filename`.
If exceeded, the file is skipped and a warning is shown, but the build still completes.
### `llms_txt_file`
- **Type**: boolean
- **Default**: `True`
- **Description**: Whether to write the summary information file
### `llms_txt_filename`
- **Type**: string
- **Default**: `llms.txt`
- **Description**: Name of the summary information file
### `llms_txt_directives`
- **Type**: list of strings
- **Default**: `[]`
- **Description**: List of custom directive names to process for path resolution.
### `llms_txt_title`
- **Type**: string or `None`
- **Default**: `None`
- **Description**: Overrides the Sphinx project name as the heading in `llms.txt`.
### `llms_txt_summary`
- **Type**: string or `None`
- **Default**: `None`
- **Description**: Optional, but recommended, summary description for `llms.txt`.
### `llms_txt_exclude`
- **Type**: list of strings
- **Default**: `[]`
- **Description**: A list of pages to ignore (e.g., `["page1", "page_with_*"]`).
## Features
- Creates `llms.txt` and `llms-full.txt`
- Automatically add content from `include` directives
- Resolves relative paths in directives like `image` and `figure` to use full paths
- Ability to add list of custom directives with `llms_txt_directives`
- Optionally, prepend a base URL using Sphinx's `html_baseurl`
- Ability to exclude pages
## License ## License
+170
View File
@@ -0,0 +1,170 @@
Advanced Configuration
======================
This page covers advanced configuration options for the sphinx-llms-txt extension.
.. _customizing_llms_files:
Customizing the LLMs Files
^^^^^^^^^^^^^^^^^^^^^^^^^^
By default, the extension generates two files:
1. ``llms.txt`` - A summary file in Markdown format
2. ``llms-full.txt`` - A complete documentation file in reStructuredText format
You can customize these files in several ways:
.. _changing_filenames:
Changing Filenames
~~~~~~~~~~~~~~~~~~
You can change the default filenames by setting these values in your ``conf.py``:
.. code-block:: python
llms_txt_filename = "custom-summary.txt"
llms_txt_full_filename = "custom-docs.txt"
.. _disabling_file_generation:
Disabling File Generation
~~~~~~~~~~~~~~~~~~~~~~~~~
If you only want one of the files, you can disable generation of the other:
.. code-block:: python
# Disable summary file
llms_txt_file = False
# Disable full documentation file
llms_txt_full_file = False
.. _custom_summary:
Adding a Custom Summary
~~~~~~~~~~~~~~~~~~~~~~~
The summary file can include a custom description of your project:
.. code-block:: python
llms_txt_summary = """
This documentation explains how to use MyProject to build amazing
applications. The project provides a comprehensive API for handling
data processing and visualization.
"""
.. note:: The summary can span multiple lines and will be properly formatted in the output file.
.. _custom_title:
Custom Title
~~~~~~~~~~~~
By default, the project name from Sphinx is used as the title in ``llms.txt``. You can override this:
.. code-block:: python
llms_txt_title = "My Custom Project Documentation"
.. _handling_large_documentation:
Handling Large Documentation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For very large documentation sets, generating the full documentation file might exceed reasonable size limits.
You can set a maximum line count:
.. code-block:: python
llms_txt_full_max_size = 10000 # Maximum 10,000 lines
If the generated file would exceed this limit, the extension will skip its generation and show a warning, allowing the build to complete.
.. tip:: Use :ref:`excluding_content` to remove less relevant pages.
.. _custom_directive_handling:
Custom Directive Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
.. _path_resolution:
Path Resolution
~~~~~~~~~~~~~~~
The extension resolves paths in the common directives ``[ 'image', 'figure']`` by default.
You can add custom directives to this list:
.. code-block:: python
llms_txt_directives = [
"my-custom-image-directive",
"another-directive-with-paths",
]
This ensures that paths in your custom directives are properly resolved in the generated files.
.. _excluding_content:
Excluding Content
^^^^^^^^^^^^^^^^^
You can exclude specific pages from being included in the generated files:
.. code-block:: python
llms_txt_exclude = [
"search", # Exclude the search page
"genindex", # Exclude the index page
"private_*", # Exclude all pages starting with 'private_'
]
This is useful for excluding auto-generated pages, indexes, or content that isn't relevant for LLM consumption.
.. _using_html_baseurl:
Using HTML Base URL
^^^^^^^^^^^^^^^^^^^
If you want to include absolute URLs for resources in your documentation, you can use Sphinx's built-in ``html_baseurl`` configuration:
.. code-block:: python
html_baseurl = "https://example.com/docs/"
When this option is set, all resolved paths in directives will be prefixed with this URL, creating absolute paths in the generated files.
.. _integration_examples:
Integration Examples
^^^^^^^^^^^^^^^^^^^^
Complete Configuration Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here's a complete example showing multiple :ref:`configuration-values`:
.. code-block:: python
# File names and generation options
llms_txt_filename = "ai-summary.txt"
llms_txt_full_filename = "ai-full-docs.txt"
llms_txt_full_max_size = 50000
# Content customization
llms_txt_title = "Project Documentation for AI Assistants"
llms_txt_summary = """
This is a comprehensive documentation set for our project.
It includes API references, usage examples, and tutorials.
"""
# Path handling
html_baseurl = "https://docs.example.com/"
llms_txt_directives = ["custom-image", "custom-include"]
# Content filtering
llms_txt_exclude = ["search", "genindex", "404", "private_*"]
+1 -1
View File
@@ -12,7 +12,7 @@ import subprocess
# -- Project information ----------------------------------------------------- # -- Project information -----------------------------------------------------
project = "Sphinx llms.txt Generator" project = "sphinx-llms-txt"
copyright = "Jared Dillard" copyright = "Jared Dillard"
author = "Jared Dillard" author = "Jared Dillard"
llms_txt_summary = """ llms_txt_summary = """
+14 -13
View File
@@ -5,7 +5,8 @@ Project Configuration Values
- **Type**: boolean - **Type**: boolean
- **Default**: ``True`` - **Default**: ``True``
- **Description**: Whether to write the single output file - **Description**: Whether to write the single output file.
See :ref:`disabling_file_generation`.
.. versionadded:: 0.1.0 .. versionadded:: 0.1.0
@@ -13,7 +14,8 @@ Project Configuration Values
- **Type**: string - **Type**: string
- **Default**: ``'llms-full.txt'`` - **Default**: ``'llms-full.txt'``
- **Description**: Name of the single output file - **Description**: Name of the single output file.
See :ref:`changing_filenames`.
.. versionadded:: 0.1.0 .. versionadded:: 0.1.0
@@ -23,6 +25,7 @@ Project Configuration Values
- **Default**: ``None`` (no limit) - **Default**: ``None`` (no limit)
- **Description**: Sets a maximum line count for ``llms_txt_full_filename``. - **Description**: Sets a maximum line count for ``llms_txt_full_filename``.
If exceeded, the file is skipped and a warning is shown, but the build still completes. If exceeded, the file is skipped and a warning is shown, but the build still completes.
See :ref:`handling_large_documentation`.
.. versionadded:: 0.2.0 .. versionadded:: 0.2.0
@@ -30,7 +33,8 @@ Project Configuration Values
- **Type**: boolean - **Type**: boolean
- **Default**: ``True`` - **Default**: ``True``
- **Description**: Whether to write the summary information file - **Description**: Whether to write the summary information file.
See :ref:`disabling_file_generation`.
.. versionadded:: 0.2.0 .. versionadded:: 0.2.0
@@ -38,7 +42,8 @@ Project Configuration Values
- **Type**: string - **Type**: string
- **Default**: ``llms.txt`` - **Default**: ``llms.txt``
- **Description**: Name of the summary information file - **Description**: Name of the summary information file.
See :ref:`changing_filenames`.
.. versionadded:: 0.2.0 .. versionadded:: 0.2.0
@@ -47,6 +52,7 @@ Project Configuration Values
- **Type**: list of strings - **Type**: list of strings
- **Default**: ``[]`` (empty list) - **Default**: ``[]`` (empty list)
- **Description**: List of custom directive names to process for path resolution. - **Description**: List of custom directive names to process for path resolution.
See :ref:`path_resolution`.
.. versionadded:: 0.1.0 .. versionadded:: 0.1.0
@@ -55,6 +61,7 @@ Project Configuration Values
- **Type**: string or ``None`` - **Type**: string or ``None``
- **Default**: ``None`` - **Default**: ``None``
- **Description**: Overrides the Sphinx project name as the heading in ``llms.txt``. - **Description**: Overrides the Sphinx project name as the heading in ``llms.txt``.
See :ref:`custom_title`.
.. versionadded:: 0.2.0 .. versionadded:: 0.2.0
@@ -63,6 +70,7 @@ Project Configuration Values
- **Type**: string or ``None`` - **Type**: string or ``None``
- **Default**: ``None`` - **Default**: ``None``
- **Description**: Optional, but recommended, summary description for ``llms.txt``. - **Description**: Optional, but recommended, summary description for ``llms.txt``.
See :ref:`custom_summary`.
.. versionadded:: 0.2.0 .. versionadded:: 0.2.0
@@ -70,14 +78,7 @@ Project Configuration Values
- **Type**: list of strings - **Type**: list of strings
- **Default**: ``[]`` - **Default**: ``[]``
- **Description**: A list of pages to ignore (e.g., ``["page1", "page_with_*"]``). - **Description**: A list of pages to ignore.
See :ref:`excluding_content`.
.. versionadded:: 0.2.1 .. versionadded:: 0.2.1
.. confval:: llms_txt_rm_directives
- **Type**: boolean
- **Default**: ``False``
- **Description**: Whether to remove all directives from the output files.
.. versionadded:: 0.2.3
+26
View File
@@ -1,6 +1,11 @@
Getting Started Getting Started
=============== ===============
Demo
----
You can see this Sphinx project's `llms.txt`_ and `llms-full.txt`_ files as a simple example.
Installation Installation
------------ ------------
@@ -22,3 +27,24 @@ Add the extension to your Sphinx configuration (``conf.py``):
] ]
Once added, the extension will automatically generate the LLMs.txt files during the build process. Once added, the extension will automatically generate the LLMs.txt files during the build process.
See :doc:`advanced-configuration` for more information about how to use **sphinx-llms-txt**.
How It Works
-----------
During the Sphinx build process:
1. **Content Collection**: Scans all of your documentation's ``_source`` pages and collects their content
2. **Directive Processing**: Resolves ``include`` directives by automatically incorporating their content
3. **Path Resolution**: Transforms relative paths in directives to full paths
4. **Output Generation**: Creates two optional files:
- ``llms.txt``: A concise summary of your documentation, in Markdown
- ``llms-full.txt``: A comprehensive version with all documentation content, in reStructuredText
5. **Content Filtering**: Allows you to exclude specific pages from the generated files
.. _llms.txt: https://sphinx-llms-txt.readthedocs.io/en/latest/llms.txt
.. _llms-full.txt: https://sphinx-llms-txt.readthedocs.io/en/latest/llms-full.txt
+1 -19
View File
@@ -9,31 +9,13 @@ A `Sphinx`_ extension that generates a summary ``llms.txt`` file, written in Mar
:maxdepth: 2 :maxdepth: 2
getting-started getting-started
advanced-configuration
configuration-values configuration-values
contributing contributing
changelog changelog
Features
--------
Sphinx LLMs.txt provides the following features:
- Creates ``llms.txt`` and ``llms-full.txt``
- Automatically add content from ``include`` directives
- Resolves relative paths in directives like ``image`` and ``figure`` to use full paths
- Ability to add list of custom directives with ``llms_txt_directives``
- Optionally, prepend a base URL using Sphinx's ``html_baseurl``
- Ability to exclude pages
Example
-------
You can see this Sphinx projects `llms.txt`_ and `llms-full.txt`_ files as a simple example.
.. _Sphinx: http://sphinx-doc.org/ .. _Sphinx: http://sphinx-doc.org/
.. _llms.txt: https://sphinx-llms-txt.readthedocs.io/en/latest/llms.txt
.. _llms-full.txt: https://sphinx-llms-txt.readthedocs.io/en/latest/llms-full.txt
.. |PyPI version| image:: https://img.shields.io/pypi/v/sphinx-llms-txt.svg .. |PyPI version| image:: https://img.shields.io/pypi/v/sphinx-llms-txt.svg
:target: https://pypi.python.org/pypi/sphinx-llms-txt :target: https://pypi.python.org/pypi/sphinx-llms-txt
+1 -1
View File
@@ -12,7 +12,7 @@ from .manager import LLMSFullManager
from .processor import DocumentProcessor from .processor import DocumentProcessor
from .writer import FileWriter from .writer import FileWriter
__version__ = "0.2.2" __version__ = "0.2.3"
# Export classes needed by tests # Export classes needed by tests
__all__ = [ __all__ = [
+27 -14
View File
@@ -65,21 +65,34 @@ class DocumentCollector:
): ):
for child_docname in self.env.toctree_includes[docname]: for child_docname in self.env.toctree_includes[docname]:
collect_from_toctree(child_docname) collect_from_toctree(child_docname)
else: # Try to use dependencies to find related documents
# Fallback: try to resolve and parse the toctree elif (
toctree = self.env.get_and_resolve_toctree(docname, None) hasattr(self.env, "dependencies")
if toctree: and docname in self.env.dependencies
from docutils import nodes ):
# Extract the dependent documents from the dependencies dict
for child_docname in self.env.dependencies[docname]:
# Only add documents actually in the document set
if (
hasattr(self.env, "all_docs")
and child_docname in self.env.all_docs
):
collect_from_toctree(child_docname)
# Fallback to titles or other available references
elif hasattr(self.env, "titles") and hasattr(self.env, "all_docs"):
# Get all document names
all_docnames = list(self.env.all_docs.keys())
for node in list(toctree.findall(nodes.reference)): # Look for documents that might be related (have similar paths)
if "refuri" in node.attributes: current_prefix = "/".join(docname.split("/")[:-1])
refuri = node.attributes["refuri"] if current_prefix:
if refuri and refuri.endswith(".html"): for child_docname in all_docnames:
child_docname = refuri[:-5] # Remove .html # Documents in the same directory might be related
if ( if (
child_docname != docname child_docname.startswith(current_prefix)
): # Avoid circular references and child_docname != docname
collect_from_toctree(child_docname) ):
collect_from_toctree(child_docname)
except Exception as e: except Exception as e:
logger.debug(f"Could not get toctree for {docname}: {e}") logger.debug(f"Could not get toctree for {docname}: {e}")
+53 -74
View File
@@ -104,14 +104,7 @@ class LLMSFullManager:
) )
return return
# Collect all available source files
txt_files = {}
for f in sources_dir.glob("**/*.txt"):
logger.debug(f"sphinx-llms-txt: Found source file: {f.stem} at {f}")
txt_files[f.stem] = f
# Log discovered files and page order # Log discovered files and page order
logger.debug(f"sphinx-llms-txt: Found {len(txt_files)} source files")
logger.debug(f"sphinx-llms-txt: Page order (after exclusion): {page_order}") logger.debug(f"sphinx-llms-txt: Page order (after exclusion): {page_order}")
# Log exclusion patterns # Log exclusion patterns
@@ -119,33 +112,28 @@ class LLMSFullManager:
if exclude_patterns: if exclude_patterns:
logger.debug(f"sphinx-llms-txt: Exclusion patterns: {exclude_patterns}") logger.debug(f"sphinx-llms-txt: Exclusion patterns: {exclude_patterns}")
# Create a mapping from docnames to actual file names # Create a mapping from docnames to source files
docname_to_file = {} docname_to_file = {}
# Try exact matches first # Process each docname in the page order
for docname in page_order: for docname in page_order:
# Skip excluded pages # Skip excluded pages
if any( if exclude_patterns and any(
self.collector._match_exclude_pattern(docname, pattern) self.collector._match_exclude_pattern(docname, pattern)
for pattern in exclude_patterns for pattern in exclude_patterns
): ):
continue continue
if docname in txt_files: # Construct expected source file path directly from docname
docname_to_file[docname] = txt_files[docname] source_file = sources_dir / f"{docname}.rst.txt"
if source_file.exists():
docname_to_file[docname] = source_file
else: else:
# Try with .rst extension logger.warning(
if f"{docname}.rst" in txt_files: f"sphinx-llm-txt: Source file not found for: {docname}. Expected"
docname_to_file[docname] = txt_files[f"{docname}.rst"] f" at {source_file}"
# Try with .txt extension )
elif f"{docname}.txt" in txt_files:
docname_to_file[docname] = txt_files[f"{docname}.txt"]
# Try with underscores instead of hyphens
elif docname.replace("-", "_") in txt_files:
docname_to_file[docname] = txt_files[docname.replace("-", "_")]
# Try with hyphens instead of underscores
elif docname.replace("_", "-") in txt_files:
docname_to_file[docname] = txt_files[docname.replace("_", "-")]
# Generate content # Generate content
content_parts = [] content_parts = []
@@ -190,65 +178,56 @@ class LLMSFullManager:
added_files.add(file_path.stem) added_files.add(file_path.stem)
total_line_count += line_count total_line_count += line_count
else: else:
logger.warning(f"sphinx-llm-txt: Source file not found for: {docname}") logger.warning(
f"sphinx-llm-txt: Source file not found for: {docname}. Check that"
f" the file exists at _sources/{docname}.rst.txt"
)
# Add any remaining files (in alphabetical order) if not aborted # Add any remaining files (in alphabetical order) that aren't in the page order
if not abort_due_to_max_lines: if not abort_due_to_max_lines:
# Apply the same exclusion filter to remaining files # Get all .rst.txt files in the _sources directory
exclude_patterns = self.config.get("llms_txt_exclude") all_source_files = list(sources_dir.glob("**/*.rst.txt"))
processed_paths = set(file.resolve() for file in docname_to_file.values())
# Create a set of files to exclude based on their basename # Find files that haven't been processed yet
excluded_files = set() remaining_source_files = [
for pattern in exclude_patterns: f for f in all_source_files if f.resolve() not in processed_paths
if "*" not in pattern and "?" not in pattern: ]
# For exact patterns, add variants
excluded_files.add(pattern)
excluded_files.add(f"{pattern}.rst")
excluded_files.add(f"{pattern}.txt")
excluded_files.add(pattern.replace("-", "_"))
excluded_files.add(pattern.replace("_", "-"))
# Filter remaining files # Sort the remaining files for consistent ordering
remaining_files = sorted( remaining_source_files.sort()
[
name if remaining_source_files:
for name in txt_files logger.info(
if name not in added_files f"Found {len(remaining_source_files)} additional files not in"
and name not in excluded_files f" toctree"
and not any( )
self.collector._match_exclude_pattern(name, pattern)
for pattern in exclude_patterns for file_path in remaining_source_files:
) # Extract docname from path by removing the .rst.txt extension
] rel_path = str(file_path.relative_to(sources_dir))
) if rel_path.endswith(".rst.txt"):
if remaining_files: docname = rel_path[:-8] # Remove .rst.txt extension
logger.info(f"Adding remaining files: {remaining_files}") else:
for file_stem in remaining_files: continue
file_path = txt_files[file_stem]
content, line_count = self._read_source_file(file_path, file_stem) # Skip excluded docnames
if exclude_patterns and any(
self.collector._match_exclude_pattern(docname, pattern)
for pattern in exclude_patterns
):
logger.debug(f"sphinx-llms-txt: Skipping excluded file: {docname}")
continue
# Read and process the file
content, line_count = self._read_source_file(file_path, docname)
# Check if adding this file would exceed the maximum line count # Check if adding this file would exceed the maximum line count
if max_lines is not None and total_line_count + line_count > max_lines: if max_lines is not None and total_line_count + line_count > max_lines:
break break
# Double-check that this file should be included if content:
should_include = True logger.debug(f"sphinx-llms-txt: Adding remaining file: {docname}")
file_stem = file_path.stem
exclude_patterns = self.config.get("llms_txt_exclude")
if exclude_patterns:
# Check stem against exclusion patterns
if any(
self.collector._match_exclude_pattern(file_stem, pattern)
for pattern in exclude_patterns
):
logger.debug(
"sphinx-llms-txt: Final exclusion check removed remaining"
f" file: {file_stem}"
)
should_include = False
if content and should_include:
content_parts.append(content) content_parts.append(content)
total_line_count += line_count total_line_count += line_count