> For the complete documentation index, see [llms.txt](https://infinitode-docs.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infinitode-docs.gitbook.io/documentation/package-documentation/localsitemap-package-documentation.md). Agents can also search and read this documentation through its [MCP server](https://infinitode-docs.gitbook.io/documentation/~gitbook/mcp).

# LocalSiteMap Package Documentation

Generate standard-compliant sitemaps and sitemap indexes from local website files.

LocalSiteMap crawls a local website directory and generates standard-compliant sitemaps and sitemap indexes. It has no third-party runtime dependencies.

## Changelog

### 2.1.0 (Latest)

**Breaking change**

* **Python 3.9 or newer is now required.** Python 3.6–3.8 are end-of-life and no longer supported by modern packaging tooling. Version 2.0.0 on PyPI still serves those interpreters, and pip selects it automatically.

**Bug fixes**

* Exclusions no longer match parent folders: they are resolved inside the site root, so a site checked out in a folder named `media` is not wiped out when `media` is excluded.
* Priority map keys behave as documented: folder keys may end with a slash (`"blog/"`), Windows separators are accepted, and extension keys are case-insensitive (`".PHP"` matches `.php`).
* URLs are percent-encoded, so names containing spaces, `#`, `&`, `?`, or non-ASCII characters (for example `my page.html` or `café/`) produce valid, crawlable URLs.
* Extension matching is case-insensitive: `PAGE.HTML` is picked up by the default configuration, and `page.html.bak` is not mistaken for `page.html`.
* Inputs are validated: a missing `root_path`, a `base_url` without a scheme, or a non-positive `max_urls` raise a clear error. `max_urls` above the protocol limit of 50,000 is clamped with a warning.
* Priorities are clamped to the `0.00`–`1.00` range required by the sitemap protocol.
* Output is deterministic and deduplicated: entries are sorted, duplicate `<loc>` values are dropped, and repeated runs produce byte-identical files.
* Stale split sitemaps are removed, so shrinking a site no longer leaves orphaned `sitemap_N.xml` parts behind.
* Output is written atomically and missing output directories are created, so an interrupted run cannot leave a truncated sitemap.
* XML writing is much faster: the DOM round-trip was replaced with a streaming writer, which also removes stray blank lines.

**New features**

* **Command line interface**: `localsitemap` and `python -m localsitemap`, with exclusions, extensions, priorities, and a JSON priority map.
* **Glob exclusions**: `["draft-*", "*.bak"]`.
* **New options**: `exclude_hidden`, `include_homepage`, and `sort_entries`.
* **`generate_sitemap()` returns the written files**, which is handy for CI and build scripts.
* **Modern packaging**: metadata lives in `pyproject.toml` with the version read from `localsitemap/_version.py`.
* **Release pipeline**: published from GitHub Actions with PyPI Trusted Publishing (OIDC) and PEP 740 attestations.

### 2.0.0

* Exclusions now require exact folder names or complete relative paths.
* Excluded directories are pruned during traversal.
* File timestamps use accurate UTC dates.
* `allowed_extensions` supports custom indexed file types.
* `priority_mapping` accepts a dictionary or callable.
* Large sitemaps split automatically and produce a sitemap index.

### Earlier releases

* **1.0.2**: Removed the unused `install_requires` declaration.
* **1.0.1**: Added `show_progress` progress output.
* **1.0.0**: Added recursive crawling and sitemap generation.

## Installation

Install LocalSiteMap with pip:

```bash
pip install --upgrade localsitemap
```

LocalSiteMap 2.1.0 requires **Python 3.9 or newer** and is tested on CPython 3.9–3.14 on Linux plus Windows. There are no runtime dependencies.

## Features

* Recursively crawls every file and subdirectory under the root directory.
* Skips excluded directories during traversal instead of walking them and filtering afterwards.
* Uses accurate UTC file modification dates for `<lastmod>`.
* Indexes `.html` and `.htm` files by default; any extension list is supported (`.php`, `.pdf`, `.txt`).
* Supports names, relative paths, absolute paths, and shell-style glob patterns as exclusions.
* Maps paths, folders, file names, and extensions to custom priorities with a dictionary or callable.
* Splits large sites into numbered sitemaps with a compliant `<sitemapindex>` parent.
* Offers both a Python API and a command line interface.

{% hint style="warning" %}
LocalSiteMap crawls every directory under the root. Exclude hidden, resource, and sensitive directories — for example `.git`, `node_modules`, and build output — or set `exclude_hidden=True`.
{% endhint %}

## Example Usage

### Generate a basic sitemap

```python
from localsitemap import generate_sitemap

# Root site directory
root_directory = r"path/to/your/website/directory"

# Domain of your website (where it is hosted)
base_url_of_your_website = "https://example.com"

# Names, relative/absolute paths, or glob patterns to skip
excluded = ["auth", "forms", "template.html", "media", ".git", ".vscode", "node_modules"]

# Generate the sitemap; the return value lists every file that was written
written = generate_sitemap(
    root_directory,
    base_url_of_your_website,
    "sitemap.xml",
    excluded,
    show_progress=True,
)
print("Sitemap generated:", ", ".join(written))
```

### Configure extensions, priorities, and splitting

```python
from localsitemap import generate_sitemap

# Use a dictionary or callable for custom priorities.
# Keys may be a relative path, a folder name (trailing slash optional),
# a file name, or an extension (case-insensitive).
priority_mapping = {
    "about/index.html": 0.95,
    "blog/": 0.70,
    ".php": 0.50,
}

generate_sitemap(
    root_path=r"path/to/website",
    base_url="https://example.com",
    output_file="sitemap.xml",
    excluded_paths=["node_modules", ".git", "draft-*", "*.bak"],
    allowed_extensions=[".html", ".htm", ".php"],
    default_priority=0.80,
    homepage_priority=1.00,
    priority_mapping=priority_mapping,
    max_urls=1000,          # Splits and writes a sitemap index above 1000 URLs
    exclude_hidden=True,
    include_homepage=True,
    sort_entries=True,
)
```

When the URL count exceeds `max_urls`, LocalSiteMap writes numbered sitemaps (`sitemap_1.xml`, `sitemap_2.xml`, …) and a `sitemap.xml` index. The returned list contains the numbered parts first and the index last. `max_urls` is capped at the 50,000-URL protocol limit.

### Exclusions

Exclusions are always interpreted relative to `root_path`:

| Pattern                | Meaning                                         |
| ---------------------- | ----------------------------------------------- |
| `"node_modules"`       | any file or folder with that name, at any depth |
| `"blog/posts"`         | that relative path and everything below it      |
| `"/srv/site/private"`  | an absolute path inside the site root           |
| `"draft-*"`, `"*.bak"` | shell-style glob patterns                       |

`excluded_paths=["auth"]` therefore excludes `auth/` but keeps `authority/`.

### Command line

```bash
# After pip install localsitemap (or with `python -m localsitemap`)
localsitemap path/to/website https://example.com \
    -o public/sitemap.xml \
    --exclude auth --exclude "draft-*" \
    --extensions .html,.htm,.php \
    --max-urls 1000 \
    --progress
```

Run `localsitemap --help` for every option, including `--priority-map priorities.json`, `--exclude-hidden`, `--no-homepage`, `--no-sort`, and `--version`.

### Release provenance

Releases are built and published by GitHub Actions through [PyPI Trusted Publishing](https://docs.pypi.org/trusted-publishers/) (OIDC) — there is no long-lived PyPI API token in the repository. Every uploaded file is signed with a [PEP 740](https://peps.python.org/pep-0740/) digital attestation, which is what makes PyPI display the provenance indicator. Verify a release with the bundled script or the public Integrity API:

```bash
python .github/scripts/verify_provenance.py --project localsitemap --version 2.1.0
```

```bash
curl -s "https://pypi.org/integrity/localsitemap/2.1.0/localsitemap-2.1.0-py3-none-any.whl/provenance" \
  | python -m json.tool
```

A non-`404` response containing an `attestation_bundles` entry proves the file was built by this repository's workflow.

## License

LocalSiteMap uses the **MIT License (Modified)**. Substantial derivative changes must use a different name and be clearly distinguished from the original.
