mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-01-29 03:55:59 +03:00
Parse admin command body templates from doc comments
This commit is contained in:
parent
677f044d13
commit
87225e70c3
1 changed files with 48 additions and 21 deletions
|
@ -132,7 +132,7 @@ impl Admin {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and process a message from the admin room
|
// Parse and process a message from the admin room
|
||||||
pub fn process_admin_message(db: &Database, room_message: String) -> RoomMessageEventContent {
|
fn process_admin_message(db: &Database, room_message: String) -> RoomMessageEventContent {
|
||||||
let mut lines = room_message.lines();
|
let mut lines = room_message.lines();
|
||||||
let command_line = lines.next().expect("each string has at least one line");
|
let command_line = lines.next().expect("each string has at least one line");
|
||||||
let body: Vec<_> = lines.collect();
|
let body: Vec<_> = lines.collect();
|
||||||
|
@ -202,7 +202,10 @@ enum AdminCommand {
|
||||||
/// Registering a new bridge using the ID of an existing bridge will replace
|
/// Registering a new bridge using the ID of an existing bridge will replace
|
||||||
/// the old one.
|
/// the old one.
|
||||||
///
|
///
|
||||||
/// [add-yaml-block-to-usage]
|
/// [commandbody]
|
||||||
|
/// # ```
|
||||||
|
/// # yaml content here
|
||||||
|
/// # ```
|
||||||
RegisterAppservice,
|
RegisterAppservice,
|
||||||
|
|
||||||
/// Unregister an appservice using its ID
|
/// Unregister an appservice using its ID
|
||||||
|
@ -225,10 +228,16 @@ enum AdminCommand {
|
||||||
event_id: Box<EventId>,
|
event_id: Box<EventId>,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
#[clap(verbatim_doc_comment)]
|
||||||
/// Parse and print a PDU from a JSON
|
/// Parse and print a PDU from a JSON
|
||||||
///
|
///
|
||||||
/// The PDU event is only checked for validity and is not added to the
|
/// The PDU event is only checked for validity and is not added to the
|
||||||
/// database.
|
/// database.
|
||||||
|
///
|
||||||
|
/// [commandbody]
|
||||||
|
/// # ```
|
||||||
|
/// # PDU json content here
|
||||||
|
/// # ```
|
||||||
ParsePdu,
|
ParsePdu,
|
||||||
|
|
||||||
/// Retrieve and print a PDU by ID from the Conduit database
|
/// Retrieve and print a PDU by ID from the Conduit database
|
||||||
|
@ -433,33 +442,51 @@ fn usage_to_html(text: &str) -> String {
|
||||||
.expect("Regex compilation should not fail");
|
.expect("Regex compilation should not fail");
|
||||||
let text = re.replace_all(&text, "<code>$1</code>: $4");
|
let text = re.replace_all(&text, "<code>$1</code>: $4");
|
||||||
|
|
||||||
// // Enclose examples in code blocks
|
// Look for a `[commandbody]` tag. If it exists, use all lines below it that
|
||||||
// // (?ms) enables multi-line mode and dot-matches-all
|
// start with a `#` in the USAGE section.
|
||||||
// let re =
|
let mut text_lines: Vec<&str> = text.lines().collect();
|
||||||
// Regex::new("(?ms)^Example:\n(.*?)\nUSAGE:$").expect("Regex compilation should not fail");
|
let mut command_body = String::new();
|
||||||
// let text = re.replace_all(&text, "EXAMPLE:\n<pre>$1</pre>\nUSAGE:");
|
|
||||||
|
|
||||||
let has_yaml_block_marker = text.contains("\n[add-yaml-block-to-usage]\n");
|
if let Some(line_index) = text_lines.iter().position(|line| *line == "[commandbody]") {
|
||||||
let text = text.replace("\n[add-yaml-block-to-usage]\n", "");
|
text_lines.remove(line_index);
|
||||||
|
|
||||||
// Add HTML line-breaks
|
while text_lines
|
||||||
let text = text.replace("\n", "<br>\n");
|
.get(line_index)
|
||||||
|
.map(|line| line.starts_with("#"))
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
command_body += if text_lines[line_index].starts_with("# ") {
|
||||||
|
&text_lines[line_index][2..]
|
||||||
|
} else {
|
||||||
|
&text_lines[line_index][1..]
|
||||||
|
};
|
||||||
|
command_body += "[nobr]\n";
|
||||||
|
text_lines.remove(line_index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let text = if !has_yaml_block_marker {
|
let text = text_lines.join("\n");
|
||||||
|
|
||||||
|
// Improve the usage section
|
||||||
|
let text = if command_body.is_empty() {
|
||||||
// Wrap the usage line in code tags
|
// Wrap the usage line in code tags
|
||||||
let re = Regex::new("(?m)^USAGE:<br>\n (@conduit:.*)<br>$")
|
let re = Regex::new("(?m)^USAGE:\n (@conduit:.*)$")
|
||||||
.expect("Regex compilation should not fail");
|
.expect("Regex compilation should not fail");
|
||||||
re.replace_all(&text, "USAGE:<br>\n<code>$1</code><br>")
|
re.replace_all(&text, "USAGE:\n<code>$1</code>").to_string()
|
||||||
} else {
|
} else {
|
||||||
// Wrap the usage line in a code block, and add a yaml block example
|
// Wrap the usage line in a code block, and add a yaml block example
|
||||||
// This makes the usage of e.g. `register-appservice` more accurate
|
// This makes the usage of e.g. `register-appservice` more accurate
|
||||||
let re = Regex::new("(?m)^USAGE:<br>\n (.*?)<br>\n<br>\n")
|
let re =
|
||||||
.expect("Regex compilation should not fail");
|
Regex::new("(?m)^USAGE:\n (.*?)\n\n").expect("Regex compilation should not fail");
|
||||||
re.replace_all(
|
re.replace_all(&text, "USAGE:\n<pre>$1[nobr]\n[commandbodyblock]</pre>")
|
||||||
&text,
|
.replace("[commandbodyblock]", &command_body)
|
||||||
"USAGE:<br>\n<pre>$1\n```\nyaml content here\n```</pre>",
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
text.to_string()
|
// Add HTML line-breaks
|
||||||
|
let text = text
|
||||||
|
.replace("\n\n\n", "\n\n")
|
||||||
|
.replace("\n", "<br>\n")
|
||||||
|
.replace("[nobr]<br>", "");
|
||||||
|
|
||||||
|
text
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue