-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
- Loading branch information
Showing
9 changed files
with
149 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::GuideSource; | ||
use anyhow::{format_err, Result}; | ||
use reqwest::StatusCode; | ||
use std::fs; | ||
use tracing::{debug, instrument}; | ||
|
||
/// Generate an HTML document with the guide content from the guide in markdown | ||
/// format available in the source provided. | ||
#[instrument(skip_all, err)] | ||
pub(crate) async fn generate_guide_html(src: &GuideSource) -> Result<Option<String>> { | ||
// Get guide in markdown format | ||
let Some(guide_md) = get_guide_md(src).await? else { | ||
return Ok(None); | ||
}; | ||
|
||
// Convert markdown to HTML | ||
let guide_html = markdown::to_html_with_options( | ||
&guide_md, | ||
&markdown::Options { | ||
compile: markdown::CompileOptions { | ||
allow_dangerous_html: true, | ||
..markdown::CompileOptions::default() | ||
}, | ||
..markdown::Options::default() | ||
}, | ||
) | ||
.map_err(|err| format_err!("{err}"))?; | ||
|
||
Ok(Some(guide_html)) | ||
} | ||
|
||
/// Get guide in markdown format. | ||
#[instrument(skip_all, err)] | ||
pub(crate) async fn get_guide_md(src: &GuideSource) -> Result<Option<String>> { | ||
// Try from file | ||
if let Some(file) = &src.guide_file { | ||
debug!(?file, "getting landscape guide from file"); | ||
return Ok(Some(fs::read_to_string(file)?)); | ||
}; | ||
|
||
// Try from url | ||
if let Some(url) = &src.guide_url { | ||
debug!(?url, "getting landscape guide from url"); | ||
let resp = reqwest::get(url).await?; | ||
if resp.status() != StatusCode::OK { | ||
return Err(format_err!( | ||
"unexpected status code getting landscape guide file: {}", | ||
resp.status() | ||
)); | ||
} | ||
return Ok(Some(resp.text().await?)); | ||
}; | ||
|
||
Ok(None) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters