Skip to main content

Display Python version, executable path, and script path when run with uv

Project description

anchorfix

PyPI - Version PyPI - Python Version Last Commit License: MIT

HTML アンカーを連番 ID に変換し、内部リンクを自動更新するツール

インストール

uv を使用する場合:

uv add anchorfix

pip を使用する場合:

pip install anchorfix

使用方法

anchorfix <htmlfile> [--prefix PREFIX]

基本的な使用例

デフォルトのプレフィックス 'a' を使用:

anchorfix input.html

カスタムプレフィックスを指定:

anchorfix input.html --prefix sec

出力をファイルにリダイレクト:

anchorfix input.html > output.html

コマンドラインオプション

ヘルプ表示:

anchorfix --help

出力例:

usage: anchorfix [-h] [--prefix PREFIX] [--version] htmlfile

HTMLアンカーを連番IDに変換

positional arguments:
  htmlfile         入力HTMLファイルパス

options:
  -h, --help       show this help message and exit
  --prefix PREFIX  アンカーIDのプレフィックス (デフォルト: a)
  -V, --version    バージョン情報を表示

バージョン表示:

anchorfix --version

または:

anchorfix -V

仕様

目的

HTML で編集できる CMS において、ヘッダテキストに基づいたアンカー ID は内部リンクが正しく動作しないことがあります。anchorfix は、これらのアンカーを #a0001 のような連番形式に変換することで、内部リンクを確実に機能させます。

要は Markdown を HTML に変換した後に使うフィルターです。

入出力

  • 入力: 単一の HTML ファイルパス(コマンドライン引数)
  • 出力: 変換された HTML を標準出力(stdout)に出力

処理対象

以下の HTML 要素を処理します:

  • <h1> - <h6> タグの id 属性
  • <a> タグの name 属性

変換ルール

  1. 連番形式: {prefix}0001, {prefix}0002, ... (4 桁の連番、0001 から開始)
  2. 既存アンカーの上書き: 既存のid/name属性値は全て上書き
  3. 内部リンク更新: 同一ファイル内の<a href="#...">を自動更新
  4. 外部リンク保持: <a href="other.html#anchor">のような外部参照は変更しない

エラー処理

  • ファイル読み込みエラー: 例外をスロー
  • 重複 ID 検出: 同じid値が複数存在する場合、行番号と具体的な ID 値を含む例外をスロー
  • 不完全な HTML: <body>の中身のみのような不完全な HTML も許容

サンプル

詳細なサンプルは examples/ ディレクトリを参照してください:

  • examples/basic_input.html / examples/basic_expected.html - 基本的な変換例
  • examples/incomplete_input.html / examples/incomplete_expected.html - 不完全な HTML
  • examples/mixed_links_input.html / examples/mixed_links_expected.html - 外部リンク混在
  • examples/duplicate_id_input.html - 重複 ID エラーケース

技術仕様

  • HTML パーサー: BeautifulSoup4
  • CLI ライブラリ: argparse (標準ライブラリ)
  • 入力エンコーディング: UTF-8 を想定し自動判定
  • 出力エンコーディング: UTF-8 固定
  • HTML フォーマット: 元のインデント・改行は保持しない

開発者向け

セットアップ

uv sync

タスク実行

# テスト実行
# テストは `examples/` ディレクトリのサンプルファイルを直接参照します
poe test

# リント・型チェック
poe check
poe mypy

# フォーマット
poe format

# 全チェック実行&ビルド&スモークテスト
poe build

Property-Based Testing

このプロジェクトでは、Hypothesis を使用した基本的な Property-Based Testing(PBT)を採用しています。

テスト対象プロパティ

  1. 一意性: 生成されたアンカー ID はすべて一意
  2. 形式正確性: すべてのアンカー ID が{prefix}\d{4}形式に一致
  3. 内部リンク整合性: <a href="#...">が変換後のアンカー ID と正しく対応

Hypothesis コード例

from hypothesis import given, strategies as st
from anchorfix import process_html

@given(st.text(min_size=1, max_size=10))
def test_anchor_uniqueness(prefix):
    """生成されたアンカーIDが一意であることを検証"""
    html = "<h2 id='a'>A</h2><h2 id='b'>B</h2>"
    result = process_html(html, prefix=prefix)
    # アンカーIDを抽出して一意性を確認
    # ...

@given(st.text(min_size=1, max_size=10))
def test_anchor_format(prefix):
    """アンカーIDが正しい形式であることを検証"""
    html = "<h2 id='test'>Test</h2>"
    result = process_html(html, prefix=prefix)
    # 正規表現で形式を確認: {prefix}\d{4}
    # ...

ライセンス

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anchorfix-0.0.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anchorfix-0.0.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file anchorfix-0.0.2.tar.gz.

File metadata

  • Download URL: anchorfix-0.0.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for anchorfix-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7fc6dc14d0a48fd2affc45e3cb09b64e783f7d56a59b013bb2089b780c0a0188
MD5 5f30b5c58c9053fe0ab42dba25568bd9
BLAKE2b-256 7c7e8b41477c296630ab5f554b514b29e88a83ce1b58a66fe0968fdd1e062e33

See more details on using hashes here.

Provenance

The following attestation bundles were made for anchorfix-0.0.2.tar.gz:

Publisher: publish-pypi.yml on heiwa4126/anchorfix

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file anchorfix-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: anchorfix-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for anchorfix-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5dd24a7e5f09754bb7ff7c1c7e78b949e1e59a2e10bfe3465017ebc824ed6a6b
MD5 8a1185ba53f17a3c4f66e48cddac1b15
BLAKE2b-256 af18943f89b34d0fd519fc77ad8b20e683234f6b1f015c745bf9b5b5bdd5789f

See more details on using hashes here.

Provenance

The following attestation bundles were made for anchorfix-0.0.2-py3-none-any.whl:

Publisher: publish-pypi.yml on heiwa4126/anchorfix

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page