Skip to content

Remove duplicate code from structure processors#6

Merged
emcd merged 5 commits into
masterfrom
claude/refactor-duplicate-code-012mREEjAec83ffAgiapFLW4
Nov 20, 2025
Merged

Remove duplicate code from structure processors#6
emcd merged 5 commits into
masterfrom
claude/refactor-duplicate-code-012mREEjAec83ffAgiapFLW4

Conversation

@emcd
Copy link
Copy Markdown
Owner

@emcd emcd commented Nov 20, 2025

Eliminates code duplication by creating a shared URL utilities module at the structures level. The normalize_base_url function was duplicated identically in both sphinx and pydoctor structure processors.

Changes:

  • Create sources/librovore/structures/urls.py with normalize_base_url
  • Update sources/librovore/structures/__.py to export shared utilities
  • Refactor sphinx/urls.py to import shared normalize_base_url
  • Refactor pydoctor/urls.py to import shared normalize_base_url
  • Add implementation tracking file for documentation

Benefits:

  • Reduces code duplication (25 lines per processor)
  • Ensures consistent URL handling across all structure processors
  • Simplifies maintenance and reduces risk of divergence
  • Makes shared utilities available to future processors

All linters pass (ruff, isort, pyright).
All 171 tests pass.

Eliminates code duplication by creating a shared URL utilities module
at the structures level. The normalize_base_url function was duplicated
identically in both sphinx and pydoctor structure processors.

Changes:
- Create sources/librovore/structures/urls.py with normalize_base_url
- Update sources/librovore/structures/__.py to export shared utilities
- Refactor sphinx/urls.py to import shared normalize_base_url
- Refactor pydoctor/urls.py to import shared normalize_base_url
- Add implementation tracking file for documentation

Benefits:
- Reduces code duplication (25 lines per processor)
- Ensures consistent URL handling across all structure processors
- Simplifies maintenance and reduces risk of divergence
- Makes shared utilities available to future processors

All linters pass (ruff, isort, pyright).
All 171 tests pass.
Corrects the import pattern for shared URL utilities and extracts
duplicate from_source method to base class.

Import Pattern Fixes:
- Remove wildcard import of urls from structures/__.py
- Add `from ..urls import *` to individual processor __.py files
- Update callers to use __.normalize_base_url instead of _urls.normalize_base_url
- Remove unused _urls imports from main.py and extraction.py files
- Simplify urls.py modules to not re-export shared functions

from_source Duplication Elimination:
- Move identical from_source implementation to StructureDetection base class
- Remove duplicate implementations from all 4 Detection classes:
  * SphinxDetection
  * PydoctorDetection
  * MkDocsDetection
  * RustdocDetection
- Eliminates 10 lines of duplicate code per class (40 lines total)

Benefits:
- Correct import hierarchy following project patterns
- Reduces code duplication and maintenance burden
- Ensures consistent behavior across all structure processors
- Prevents implementation drift between Detection classes

All linters pass (ruff, isort, pyright).
All 171 tests pass.
Ensures all four structure processors use the same shared normalize_base_url
from sources/librovore/structures/urls.py.

Before this fix:
- Sphinx & Pydoctor: Used shared normalize_base_url from structures/urls.py
- Mkdocs & Rustdoc: Used different normalize_base_url from librovore/urls.py (via xtnsapi)

The two implementations had subtle differences that could lead to inconsistent
behavior. This fix ensures all structure processors use the same shared
implementation.

Changes:
- Add `from ..urls import *` to mkdocs/__.py
- Add `from ..urls import *` to rustdoc/__.py

All linters pass (ruff, isort, pyright).
All 171 tests pass.
@emcd emcd merged commit d2717ee into master Nov 20, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants