Skip to content

Commit

Permalink
fix for text content edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 7, 2019
1 parent 2aa9139 commit 31e7e83
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ path = "src/main.rs"
[dependencies]
futures = "0.3"
itertools = "0.8"
pulldown-cmark = "0.6"
reqwest = { version = "=0.10.0-alpha.2", features = ["json", "tls"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
8 changes: 6 additions & 2 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ impl Bot {
}
}

pub async fn send_message<S: Into<String>>(&self, text: S) -> Result<(), BotError> {
pub async fn send_message<S, M>(&self, text: S, parse_mode: M) -> Result<(), BotError>
where
S: Into<String>,
M: Into<Option<&'static str>>,
{
let client = reqwest::Client::new();
let TelegramResponse { ok, description } = client
.post(&self.url)
.json(&SendMessage {
chat_id: self.chat_id.clone(),
text: text.into(),
parse_mode: "Markdown",
parse_mode: parse_mode.into().unwrap_or("Markdown"),
disable_notification: true,
})
.send()
Expand Down
52 changes: 39 additions & 13 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub async fn execute(bot: &Bot) -> Result<(), DiffError> {
let head = head?;
let diff = diff.map(analyze_diff)?;

bot.send_message(head + "\n" + &diff)
bot.send_message(head + "\n" + &diff, "HTML")
.await
.map_err(DiffError::from)
}
Expand Down Expand Up @@ -78,15 +78,15 @@ fn analyze_diff(
let back: &'static str;

if yarn {
front = "先执行 `yarn`,然后执行 `pwsh ./scripts/build.ps1`。";
front = "先执行 <code>yarn</code>,然后执行 <code>pwsh ./scripts/build.ps1</code>。";
} else if build {
front = "执行 `pwsh ./scripts/build.ps1`。";
front = "执行 <code>pwsh ./scripts/build.ps1</code>。";
} else {
front = "";
}

if composer {
back = "执行 `composer install`。";
back = "执行 <code>composer install</code>。";
} else {
back = "";
}
Expand All @@ -96,10 +96,27 @@ fn analyze_diff(

async fn head() -> Result<String, DiffError> {
let command = Command::new("git")
.args(&["log", "--pretty=*%h*: %s", "-1"])
.args(&["log", "--pretty=**%h**: %s", "-1"])
.output()?;

String::from_utf8(command.stdout).map_err(DiffError::from)
String::from_utf8(command.stdout)
.map(md2html)
.map_err(DiffError::from)
}

fn md2html(text: String) -> String {
use pulldown_cmark::{html, Parser};

let text = text.replace("<", "&lt;").replace(">", "&gt;");
let parser = Parser::new(&text);
let mut output = String::new();
html::push_html(&mut output, parser);

output
.trim()
.trim_start_matches("<p>")
.trim_end_matches("</p>")
.into()
}

#[test]
Expand All @@ -114,18 +131,18 @@ fn test_analyze_diff() {

diff.yarn = true;
let analysis = analyze_diff(diff.clone());
assert!(analysis.contains("`yarn`"));
assert!(analysis.contains("yarn"));
assert!(analysis.contains("pwsh"));

diff.yarn = false;
diff.build = true;
let analysis = analyze_diff(diff.clone());
assert!(!analysis.contains("`yarn`"));
assert!(!analysis.contains("yarn"));
assert!(analysis.contains("pwsh"));

diff.composer = true;
let analysis = analyze_diff(diff.clone());
assert!(analysis.contains("`composer install`"));
assert!(analysis.contains("composer install"));
}

#[test]
Expand All @@ -135,16 +152,25 @@ fn test_head() {
let mut runtime = Runtime::new().unwrap();
runtime.block_on(async move {
let output = head().await.unwrap();
assert!(output.ends_with('\n'));
assert!(!output.contains("<p>"));
assert!(!output.contains("</p>"));

let parts = output.split(':').collect::<Vec<&str>>();
let left = parts[0];
let right = parts[1];

assert!(left.starts_with('*'));
assert!(left.ends_with('*'));
assert_eq!(left.len(), 9);
assert!(left.starts_with("<strong>"));
assert!(left.ends_with("</strong>"));
assert_eq!(left.len(), 24);

assert!(right.starts_with(' '));
});
}

#[test]
fn test_md2html() {
assert_eq!("&lt;modal&gt;", &md2html(String::from("<modal>")));
assert_eq!("&quot;text&quot;", &md2html(String::from("\"text\"")));
assert_eq!("&amp;", &md2html(String::from("&")));
assert_eq!("<strong>bold</strong>", &md2html(String::from("**bold**")));
}
4 changes: 3 additions & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ pub async fn execute<P: AsRef<Path>>(bot: &Bot, path: P) -> Result<(), PluginDat
.join("\n");
let text = format!("插件更新:\n{}", list);

bot.send_message(text).await.map_err(PluginDataError::from)
bot.send_message(text, None)
.await
.map_err(PluginDataError::from)
}

#[test]
Expand Down

0 comments on commit 31e7e83

Please sign in to comment.