Skip to content

Commit

Permalink
Fix bad module name parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 committed Jul 27, 2020
1 parent 2f0125d commit eabf851
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Versions and bullets are arranged chronologically from latest to oldest.

## v0.2.1

- Fix bad module name parsing

## v0.2.0

- Add a new command `upgrade`
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="legunto",
version="0.2.0",
version="0.2.1",
description="Fetch MediaWiki Scribunto modules from wikis",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
11 changes: 3 additions & 8 deletions src/scribunto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@


def search_dependencies(text: str, prefix=None) -> List[str]:
regex = \
r'''(?:require|mw\.loadData)\s*\(\s*['"](?:[Mm]odule|모듈):(.+)['"]\s*'''
regex = r'''(?:require|mw\.loadData)\s*\(\s*['"](?:[Mm]odule|모듈):([^'"]+)['"]''' # noqa: E501

find = re.findall(regex, text)
find = list(set(find))
Expand All @@ -19,18 +18,14 @@ def search_dependencies(text: str, prefix=None) -> List[str]:

def rewrite_requires(text: str, prefix: str) -> str:
# Module:foo/bar -> Module:foo-bar
regex = \
r'''(?:require|mw\.loadData)\s*\(\s*['"](?:[Mm]odule|모듈):.+['"]\s*'''
regex = r'''(?:require|mw\.loadData)\s*\(\s*['"](?:[Mm]odule|모듈):[^'"]+['"]''' # noqa: E501

module_names = re.findall(regex, text)
for name in module_names:
text = text.replace(name, name.replace("/", "-"))

# Module:foo -> Module:@en/foo
regex = (
r"""((?:require|mw\.loadData)\s*\(\s*['"](?:[Mm]odule|모듈):)"""
r"""(.+)(['"]\s*)"""
)
regex = r"""((?:require|mw\.loadData)\s*\(\s*['"](?:[Mm]odule|모듈):)([^'"]+)(['"])""" # noqa: E501

text = re.sub(regex, fr'\1@{prefix}/\2\3', text)

Expand Down
6 changes: 6 additions & 0 deletions test/scribunto_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def test_search_dependencies() -> None:

assert actual.sort() == expected.sort()

text = 'require("Module:i18n").loadI18n("Module:WikidataIB/i18n", i18n)'
expected = ['@en/i18n']
actual = scribunto.search_dependencies(text, 'en')

assert actual == expected


def test_rewrite_requires() -> None:
text = '''
Expand Down

0 comments on commit eabf851

Please sign in to comment.