diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8adc05e..c7da899 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,9 +8,7 @@ repos: - id: check-added-large-files - repo: local hooks: - - id: pylint - name: pylint - entry: pylint + - id: litwiki-tests + name: litwiki-tests + entry: ./test.sh language: system - types: [python] - require_serial: true diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..b4be1e2 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +orgparse = "*" + +[dev-packages] + +[requires] +python_version = "3.11" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..c043628 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,29 @@ +{ + "_meta": { + "hash": { + "sha256": "22f5663e77fd5f550bbb84f958573b3be232bb1211f2944c495839868e6f8553" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.11" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "orgparse": { + "hashes": [ + "sha256:451050e79acb7a51c65dc99b9095eae4d50bd598541354f9e763cb4cbdf59a55", + "sha256:f8c8b6c07e8c5a99a27ad3962eedab3aa3844129cbdfd3f2cd32a2197da462fe" + ], + "index": "pypi", + "version": "==0.3.2" + } + }, + "develop": {} +} diff --git a/litwiki/citation.py b/litwiki/citation.py new file mode 100644 index 0000000..d3770c6 --- /dev/null +++ b/litwiki/citation.py @@ -0,0 +1,36 @@ +import unittest +from typing import Optional +import dataclasses + + +@dataclasses.dataclass +class Citation: + volume: int + chapter: int + line: int + + +def parse(src: str) -> Optional[Citation]: + try: + volume, chapter, line = src.split(".") + volume = int(volume) + chapter = int(chapter) + line = int(line) + if volume <= 0 or chapter <= 0 or line <= 0: + raise ValueError(f"Invalid citation point {volume}.{chapter}.{line}") + return Citation(volume, chapter, line) + except ValueError: + return None + + +class TestCitation(unittest.TestCase): + def test_parse(self): + self.assertEqual(parse("1.2.3"), Citation(1, 2, 3)) + self.assertEqual(parse("1.2.0"), None) + self.assertEqual(parse("(I.2.3)"), None) + self.assertEqual(parse("I.5.1"), None) + self.assertEqual(parse(""), None) + + +if __name__ == "__main__": + unittest.main() diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..6e1e79f --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +python -m litwiki.citation