rougail-output-doc/src/rougail/output_doc/output/asciidoc.py

126 lines
3.1 KiB
Python
Raw Normal View History

2024-11-01 11:17:14 +01:00
"""
Silique (https://www.silique.fr)
2025-02-10 09:52:12 +01:00
Copyright (C) 2024-2025
2024-11-01 11:17:14 +01:00
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
2024-07-10 21:27:48 +02:00
from typing import List
from ..utils import CommonFormater, dump, to_phrase
2024-07-10 21:27:48 +02:00
2024-11-15 08:13:45 +01:00
class Formater(CommonFormater):
"""The asciidoc formater"""
2024-11-20 21:12:56 +01:00
2024-11-01 11:17:14 +01:00
name = "asciidoc"
2024-11-20 21:12:56 +01:00
_table_name = "asciidoc"
2024-07-10 21:27:48 +02:00
level = 40
2024-11-01 11:17:14 +01:00
def title(
self,
title: str,
level: int,
) -> str:
2024-11-15 08:13:45 +01:00
"""Display family name as a title"""
2024-07-10 21:27:48 +02:00
char = "="
return f"{char * (level + 1)} {title}\n\n"
2024-11-01 11:17:14 +01:00
def join(
self,
lst: List[str],
) -> str:
2024-11-15 08:13:45 +01:00
"""Display line in table from a list"""
2024-07-10 21:27:48 +02:00
string = ""
2024-11-01 11:17:14 +01:00
previous = ""
2024-07-10 21:27:48 +02:00
for line in lst:
if string:
2024-11-15 08:13:45 +01:00
if self.is_list(previous.split("\n", 1)[-1]):
2024-07-10 21:27:48 +02:00
string += "\n\n"
else:
string += " +\n"
string += line
2024-11-01 11:17:14 +01:00
2024-07-10 21:27:48 +02:00
previous = line
return "\n" + string
2024-11-15 08:13:45 +01:00
def bold(
self,
msg: str,
) -> str:
"""Set a text to bold"""
return f"**{msg}**"
def italic(
self,
msg: str,
) -> str:
"""Set a text to italic"""
return f"__{msg}__"
def stripped(
2024-11-01 11:17:14 +01:00
self,
text: str,
) -> str:
2024-11-15 08:13:45 +01:00
"""Return stripped text (as help)"""
return text.strip()
2024-07-10 21:27:48 +02:00
2024-11-15 08:13:45 +01:00
def list(
2024-11-01 11:17:14 +01:00
self,
2024-11-15 08:13:45 +01:00
choices: list,
) -> str:
"""Display a liste of element"""
prefix = "\n\n* "
char = "\n* "
return prefix + char.join([dump(choice) for choice in choices])
2024-11-01 11:17:14 +01:00
2024-11-15 08:13:45 +01:00
def prop(
2024-11-01 11:17:14 +01:00
self,
2024-11-15 08:13:45 +01:00
prop: str,
italic: bool,
2024-11-01 11:17:14 +01:00
) -> str:
2024-11-15 08:13:45 +01:00
"""Display property"""
if italic:
prop = self.italic(prop)
return f"`{prop}`"
2024-07-10 21:27:48 +02:00
2024-11-15 08:13:45 +01:00
def yaml(self, _dump: dict) -> str:
"""Dump yaml part of documentation"""
return f"[,yaml]\n----\n---\n{dump(_dump)}\n----\n"
def table(self, datas) -> str:
"""Transform list to a table in string format
we change the first line because we want that col has the same width
"""
table = super().table(datas)
stable = table.split("\n", 1)
return '[cols="1a,1a"]\n' + stable[1]
def link(
2024-11-01 11:17:14 +01:00
self,
2024-11-15 08:13:45 +01:00
comment: str,
link: str,
) -> str:
"""Add a link"""
return f"`{link}[{comment}]`"
def is_list(
self,
txt: str,
2024-11-01 11:17:14 +01:00
) -> str:
2024-11-15 08:13:45 +01:00
"""verify if a text is a list"""
return txt.strip().startswith("* ")
def to_phrase(self, text: str) -> str:
return to_phrase(text)