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.5.tar.gz (6.5 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.5-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anchorfix-0.0.5.tar.gz
  • Upload date:
  • Size: 6.5 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.5.tar.gz
Algorithm Hash digest
SHA256 41c65accf9f2176fb4ca7821589fe3f69eda9628d01944cfbe89b043a4a98f56
MD5 f648a4d84989f3f1c93085dde4f9a5b4
BLAKE2b-256 da7efb112875e123e212343905392065cfc340b8ef9b6aeb500dffafc81125cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for anchorfix-0.0.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: anchorfix-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.9 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 37a2cacfb445adf241f98d3b4d601c2855b493733a26fe847c624c1c1e6ff732
MD5 d8280015e179f913cbd158f46e6d90c8
BLAKE2b-256 72e9a5fd2922db0c217959a0431217793475f88ff91af2e668f0f833c185b360

See more details on using hashes here.

Provenance

The following attestation bundles were made for anchorfix-0.0.5-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