3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-06 14:13:23 +00:00

cmdref: codeblock language now works

Add `options` map for setting `ContentListing` options with key:val pairs; currently only used with `ContentListing::codeblock()` language arg.
Fix generated codeblock rst to print each line of code with indentation, and change it to use explicit an `code-block` so we can set a language on it.
This commit is contained in:
Krystine Sherwin 2025-03-21 10:28:48 +13:00
parent fb944ca0fb
commit b1c9097a12
No known key found for this signature in database
4 changed files with 17 additions and 5 deletions

View file

@ -47,7 +47,8 @@ The ``formatted_help()`` method
added automatically added automatically
* ``codeblock`` content is displayed verbatim, use line breaks as desired; * ``codeblock`` content is displayed verbatim, use line breaks as desired;
takes an optional ``language`` argument for assigning the language in RST takes an optional ``language`` argument for assigning the language in RST
output for code syntax highlighting (currently not implemented) output for code syntax highlighting (use ``yoscrypt`` for yosys script
syntax highlighting)
* ``option`` lists a single option for the command, usually starting with a * ``option`` lists a single option for the command, usually starting with a
dash (``-``); takes an optional second argument which adds a paragraph dash (``-``); takes an optional second argument which adds a paragraph
node as a means of description node as a means of description
@ -62,8 +63,6 @@ The ``formatted_help()`` method
+ paragraphs are treated as raw RST, allowing for inline formatting and + paragraphs are treated as raw RST, allowing for inline formatting and
references as if it were written in the RST file itself references as if it were written in the RST file itself
.. todo:: Support ``ContentListing::codeblock`` language argument
.. todo:: Support anonymous optiongroup .. todo:: Support anonymous optiongroup
If an option is added to the root node it should add the option to the last If an option is added to the root node it should add the option to the last

View file

@ -28,6 +28,7 @@ class YosysCmdContentListing:
body: str body: str
source_file: str source_file: str
source_line: int source_line: int
options: dict[str, str]
content: list[YosysCmdContentListing] content: list[YosysCmdContentListing]
def __init__( def __init__(
@ -36,12 +37,14 @@ class YosysCmdContentListing:
body: str = "", body: str = "",
source_file: str = "unknown", source_file: str = "unknown",
source_line: int = 0, source_line: int = 0,
options: dict[str, str] = {},
content: list[dict[str]] = [], content: list[dict[str]] = [],
): ):
self.type = type self.type = type
self.body = body self.body = body
self.source_file = source_file self.source_file = source_file
self.source_line = source_line self.source_line = source_line
self.options = options
self.content = [YosysCmdContentListing(**c) for c in content] self.content = [YosysCmdContentListing(**c) for c in content]
class YosysCmd: class YosysCmd:
@ -375,9 +378,11 @@ class YosysCmdDocumenter(YosysCmdGroupDocumenter):
self.add_line(f'{indent_str}{content_list.body}', content_source) self.add_line(f'{indent_str}{content_list.body}', content_source)
self.add_line('', source_name) self.add_line('', source_name)
elif content_list.type == 'code': elif content_list.type == 'code':
self.add_line(f'{indent_str}::', source_name) language_str = content_list.options.get('language', '')
self.add_line(f'{indent_str}.. code-block:: {language_str}', source_name)
self.add_line('', source_name) self.add_line('', source_name)
self.add_line(f'{indent_str} {content_list.body}', content_source) for body_line in content_list.body.splitlines():
self.add_line(f'{indent_str} {body_line}', content_source)
self.add_line('', source_name) self.add_line('', source_name)
else: else:
logger.warning(f"unknown content type '{content_list.type}'") logger.warning(f"unknown content type '{content_list.type}'")

View file

@ -27,6 +27,7 @@ Json ContentListing::to_json() {
if (body.length()) object["body"] = body; if (body.length()) object["body"] = body;
if (strcmp(source_file, "unknown") != 0) object["source_file"] = source_file; if (strcmp(source_file, "unknown") != 0) object["source_file"] = source_file;
if (source_line != 0) object["source_line"] = source_line; if (source_line != 0) object["source_line"] = source_line;
object["options"] = Json(options);
Json::array content_array; Json::array content_array;
for (auto child : _content) content_array.push_back(child->to_json()); for (auto child : _content) content_array.push_back(child->to_json());
object["content"] = content_array; object["content"] = content_array;
@ -52,6 +53,7 @@ void ContentListing::codeblock(const string &code, const string &language,
const source_location location) const source_location location)
{ {
add_content("code", code, location); add_content("code", code, location);
back()->set_option("language", language);
} }
void ContentListing::paragraph(const string &text, void ContentListing::paragraph(const string &text,

View file

@ -32,12 +32,14 @@ public:
string body; string body;
const char* source_file; const char* source_file;
int source_line; int source_line;
std::map<string, string> options;
ContentListing( ContentListing(
string type = "root", string body = "", string type = "root", string body = "",
const char* source_file = "unknown", int source_line = 0 const char* source_file = "unknown", int source_line = 0
) : type(type), body(body), source_file(source_file), source_line(source_line) { ) : type(type), body(body), source_file(source_file), source_line(source_line) {
_content = {}; _content = {};
options = {};
} }
ContentListing(string type, string body, source_location location) : ContentListing(string type, string body, source_location location) :
@ -61,6 +63,10 @@ public:
return _content.back(); return _content.back();
} }
void set_option(string key, string val = "") {
options[key] = val;
}
void usage( void usage(
const string &usage, const string &usage,
const source_location location = source_location::current() const source_location location = source_location::current()