Beispiel #1
0
    def __init__(self, path):
        self.theme = "default"
        sections_list = []
        for section in ["text", "prompt"]:
            sections_list.append(sections[section])

        book = Book(root=path)
        book.parse()
        chapters = book.chapters[1]
        chapters.parse()

        contents = chapters.contents[0]
        render = self.get_label(contents)
        label = Label(merge_formatted_text(render))

        self.container = HSplit(
            [
                VSplit(
                    [
                        sections["menu"],
                        sections["vseparator"],
                        HSplit([label, sections["prompt"]]),
                    ]
                ),
                sections["hseparator"],
                sections["status"],
            ]
        )
Beispiel #2
0
class TestBook:
    def setup_method(self):
        self.book = Book(root=books_path / "example")

    def test_book_metadata(self):
        self.book.parse()
        metadata = {
            "language": "en",
            "title": "Basic Introducction to Python",
            "description": "Basic introduction to Python",
            "created": "16/10/2020",
            "updated": "16/10/2020",
            "authors": ["Santos Gallegos"],
            "contents": {
                "Introducction": "intro.rst",
                "Nested Content": "nested.rst",
            },
        }
        assert self.book.metadata == metadata

    def test_book_chapters(self):
        self.book.parse()

        chapters = self.book.chapters
        assert len(chapters) == 2

        chapter_one = chapters[0]
        assert isinstance(chapter_one, BookChapter)
        assert chapter_one.title == "Introducction"
        assert chapter_one.file.name == "intro.rst"

        chapter_two = chapters[1]
        assert isinstance(chapter_two, BookChapter)
        assert chapter_two.title == "Nested Content"
        assert chapter_two.file.name == "nested.rst"
Beispiel #3
0
class TestBookChapter:
    def setup_method(self):
        self.book = Book(root=books_path / "example")
        self.book.parse(all=True)
        self.chapter_one = self.book.chapters[0]
        self.chapter_two = self.book.chapters[1]

    def test_chapter_metadata(self):
        metadata = {
            "tags": "comments",
            "level": "easy",
        }
        assert self.chapter_one.metadata == metadata

        metadata = {
            "tags": "nested content, toc",
            "level": "medium",
        }
        assert self.chapter_two.metadata == metadata

    def test_chapter_toc(self):
        toc = [
            ("Comments", []),
        ]
        assert self.chapter_one.toc() == toc

        toc = [
            (
                "I'm a title",
                [
                    ("I'm a subtitle", []),
                    ("I'm another subtitle", []),
                ],
            )
        ]
        assert self.chapter_two.toc() == toc

    def test_chapter_toc_custom_min_depth(self):
        toc = [("I'm a title", [])]
        assert self.chapter_two.toc(depth=1) == toc

    def test_chapter_toc_custom_max_depth(self):
        toc = [
            (
                "I'm a title",
                [
                    ("I'm a subtitle", []),
                    (
                        "I'm another subtitle",
                        [("Another one", [])],
                    ),
                ],
            )
        ]
        assert self.chapter_two.toc(depth=99) == toc
Beispiel #4
0
    def _read_books(self, config):
        """
        Load all books from the Lira configuration file.

        Each book can be a dotted path to the module of the book,
        or a local path to the directory of the book
        (it can be a relative path to the config file).

        .. code-block:: yaml
           books:
             # A dotted path to the module
             - lira.books.intro

             # A local path
             - ~/local/path/to/book/

             # A relative path to the config file
             - relative/path

             # Install from a package (maybe in the future)
             - name: lira.book.basic
               package: git+https://gifhub.com/pythonecuador/pythones-lirabook
             - name: lira.book.basic
               package: pythones-lirabook
        """
        books_list = []
        for book_path in config.get("books", []):
            path = Path(book_path).expanduser()
            if not path.is_absolute():
                path = (CONFIG_FILE.parent / path).resolve()

            if path.exists() and path.is_dir():
                books_list.append(Book(root=path))
            else:
                try:
                    package = importlib.import_module(book_path)
                    path = Path(package.__file__).parent
                    books_list.append(Book(root=path))
                except ModuleNotFoundError:
                    log.warning("Unable to find book: %s", book_path)
                except Exception as e:
                    log.warning(
                        "Unable to load book. path=%s error=%s",
                        book_path,
                        str(e),
                    )
        return books_list
Beispiel #5
0
 def setup_method(self):
     self.book = Book(root=books_path / "example")
Beispiel #6
0
 def setup_method(self):
     self.book = Book(root=books_path / "example")
     self.book.parse(all=True)
     self.chapter_one = self.book.chapters[0]
     self.chapter_two = self.book.chapters[1]
Beispiel #7
0
 def setup_method(self):
     book = Book(root=books_path / "renderer")
     book.parse(all=True)
     self.tui = mock.MagicMock()
     self.chapters = book.chapters
Beispiel #8
0
class TestBookChapter:
    def setup_method(self):
        self.book = Book(root=books_path / "example")
        self.book.parse(all=True)
        self.chapter_one = self.book.chapters[0]
        self.chapter_two = self.book.chapters[1]

    def assert_toc(self, toc, expected_toc):
        """Recursively check that `toc` has the same hierarchy as `expected`."""
        if isinstance(expected_toc, list):
            assert isinstance(toc, list)
            assert len(toc) == len(expected_toc)
            for element, expected_element in zip(toc, expected_toc):
                self.assert_toc(element, expected_element)
        else:
            section = toc[0]
            content = toc[1]
            assert section.options.title == expected_toc[0]
            self.assert_toc(content, expected_toc[1])

    def test_chapter_metadata(self):
        metadata = {
            "tags": "comments",
            "level": "easy",
        }
        assert self.chapter_one.metadata == metadata

        metadata = {
            "tags": "nested content, toc",
            "level": "medium",
        }
        assert self.chapter_two.metadata == metadata

    def test_chapter_toc(self):
        toc = [
            ("Comments", []),
        ]
        self.assert_toc(
            self.chapter_one.toc(),
            toc,
        )

        toc = [(
            "I'm a title",
            [
                ("I'm a subtitle", []),
                ("I'm another subtitle", []),
            ],
        )]
        self.assert_toc(
            self.chapter_two.toc(),
            toc,
        )

    def test_chapter_toc_custom_min_depth(self):
        toc = [("I'm a title", [])]
        self.assert_toc(
            self.chapter_two.toc(depth=1),
            toc,
        )

    def test_chapter_toc_custom_max_depth(self):
        toc = [(
            "I'm a title",
            [
                ("I'm a subtitle", []),
                (
                    "I'm another subtitle",
                    [("Another one", [])],
                ),
            ],
        )]
        self.assert_toc(
            self.chapter_two.toc(depth=99),
            toc,
        )

    def test_chapter_repr(self):
        assert str(self.chapter_one) == "<BookChapter: Introduction>"
        assert str(self.chapter_two) == "<BookChapter: Nested Content>"