def make_installation_instructions(options: Dict[str, Any], env: BuildEnvironment) -> List[str]: """ Make the content of an installation node. :param options: :param env: The Sphinx build environment. """ tabs: Dict[str, List[str]] = _get_installation_instructions(options, env) if not tabs: warnings.warn("No installation source specified. No installation instructions will be shown.") return [] content = StringList([".. tabs::", '']) content.set_indent_type(" ") for tab_name, tab_content in tabs.items(): with content.with_indent_size(1): content.append(f".. tab:: {tab_name}") content.blankline(ensure_single=True) with content.with_indent_size(2): content.extend([f"{line}" if line else '' for line in tab_content]) return list(content)
def test_indent_type(self): sl = StringList(['', '', "hello", "world", '', '', '', "1234"]) assert sl.indent_type == '\t' with pytest.raises(ValueError, match="'type' cannot an empty string."): sl.indent_type = '' assert sl.indent_type == '\t' sl.indent_type = ' ' assert sl.indent_type == ' ' sl.set_indent_type('\t') assert sl.indent_type == '\t' sl.set_indent_type(' ') assert sl.indent_type == ' ' with pytest.raises(ValueError, match="'type' cannot an empty string."): sl.set_indent_type('') assert sl.indent_type == ' ' sl.set_indent_type() assert sl.indent_type == '\t'
def test_indent(self): sl = StringList() sl.set_indent_size(1) sl.append("Indented") assert sl == ["\tIndented"] sl.set_indent_type(" ") sl.append("Indented") assert sl == ["\tIndented", " Indented"] expected_list = [ "class Foo:", '', "\tdef bar(self, listicle: List[Item]):", "\t\t...", '', "\tdef __repr__(self) -> str:", '\t\treturn "Foo()"', '', ] expected_string = dedent("""\ class Foo: def bar(self, listicle: List[Item]): ... def __repr__(self) -> str: return "Foo()" """) sl = StringList() sl.append("class Foo:") sl.blankline(True) sl.set_indent_size(1) sl.append("def bar(self, listicle: List[Item]):") sl.indent_size += 1 sl.append("...") sl.indent_size -= 1 sl.blankline(True) sl.append("def __repr__(self) -> str:") sl.indent_size += 1 sl.append('return "Foo()"') sl.indent_size -= 1 sl.blankline(True) sl.set_indent_size(0) assert sl == expected_list assert str(sl) == expected_string assert sl == expected_string sl = StringList() sl.append("class Foo:") sl.blankline(True) with sl.with_indent('\t', 1): sl.append("def bar(self, listicle: List[Item]):") with sl.with_indent('\t', 2): sl.append("...") sl.blankline(True) sl.append("def __repr__(self) -> str:") with sl.with_indent('\t', 2): sl.append('return "Foo()"') sl.blankline(True) assert sl.indent_size == 0 assert sl == expected_list assert str(sl) == expected_string assert sl == expected_string sl = StringList() sl.append("class Foo:") sl.blankline(True) with sl.with_indent_size(1): sl.append("def bar(self, listicle: List[Item]):") with sl.with_indent_size(2): sl.append("...") sl.blankline(True) sl.append("def __repr__(self) -> str:") with sl.with_indent_size(2): sl.append('return "Foo()"') sl.blankline(True) assert sl.indent_size == 0 assert sl == expected_list assert str(sl) == expected_string assert sl == expected_string sl = StringList() sl.append("class Foo:") sl.set_indent(Indent(0, " ")) sl.blankline(True) with sl.with_indent_size(1): sl.append("def bar(self, listicle: List[Item]):") with sl.with_indent_size(2): sl.append("...") sl.blankline(True) sl.append("def __repr__(self) -> str:") with sl.with_indent_size(2): sl.append('return "Foo()"') sl.blankline(True) assert sl.indent_size == 0 assert sl == [x.expandtabs(4) for x in expected_list] assert str(sl) == expected_string.expandtabs(4) assert sl == expected_string.expandtabs(4) sl = StringList() sl.append("class Foo:") sl.set_indent(" ", 0) sl.blankline(True) with sl.with_indent_size(1): sl.append("def bar(self, listicle: List[Item]):") with sl.with_indent_size(2): sl.append("...") sl.blankline(True) sl.append("def __repr__(self) -> str:") with sl.with_indent_size(2): sl.append('return "Foo()"') sl.blankline(True) assert sl.indent_size == 0 assert sl == [x.expandtabs(4) for x in expected_list] assert str(sl) == expected_string.expandtabs(4) assert sl == expected_string.expandtabs(4) sl = StringList() sl.append("class Foo:") sl.blankline(True) with sl.with_indent_size(1): sl.append("def bar(self, listicle: List[Item]):") with sl.with_indent_size(2): sl.append("...") sl.blankline(True) sl.append("def __repr__(self) -> str:") with sl.with_indent_size(2): with sl.with_indent_type(" "): sl.append('return "Foo()"') sl.blankline(True) assert sl.indent_size == 0 expected_list[-2] = ' return "Foo()"' assert sl == expected_list assert str(sl) == expected_string.replace('\t\treturn "Foo()"', ' return "Foo()"') assert sl == expected_string.replace('\t\treturn "Foo()"', ' return "Foo()"')