Skip to content

Commit

Permalink
Merge branch '0.1.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 17, 2021
2 parents 5963c07 + 948691a commit 7c6dd87
Show file tree
Hide file tree
Showing 9 changed files with 1,537 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/actions-rs/grcov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ ignore:
- "src/ui/*"
- src/main.rs
- src/helpers/open.rs
- src/helpers/strings/lookup.rs
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# Changelog

- [Changelog](#changelog)
- [0.1.1](#011)
- [0.1.0](#010)

---

## 0.1.1

Released on 17/11/2021

- Escape HTML entities from text (e.g. `&` or `“`)

## 0.1.0

Released on 15/11/2021
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT"
name = "tuifeed"
readme = "README.md"
repository = "https://github.com/veeso/tuifeed"
version = "0.1.0"
version = "0.1.1"

[[bin]]
name = "tuifeed"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tuifeed

<p align="center">Developed by <a href="https://veeso.github.io/" target="_blank">@veeso</a></p>
<p align="center">Current version: 0.1.0 (15/11/2021)</p>
<p align="center">Current version: 0.1.1 (17/11/2021)</p>
<p align="center">~ A terminal news feed reader with a fancy ui ~</p>

<p align="center">
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# -f, -y, --force, --yes
# Skip the confirmation prompt during installation

TUIFEED_VERSION="0.1.0"
TUIFEED_VERSION="0.1.1"
GITHUB_URL="https://github.com/veeso/tuifeed/releases/download/v${TUIFEED_VERSION}"
DEB_URL="${GITHUB_URL}/tuifeed_${TUIFEED_VERSION}_amd64.deb"
RPM_URL="${GITHUB_URL}/tuifeed-${TUIFEED_VERSION}-1.x86_64.rpm"
Expand Down
6 changes: 4 additions & 2 deletions src/feed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ impl From<RssFeed> for Feed {
impl From<RssEntry> for Article {
fn from(entry: RssEntry) -> Self {
Self {
title: entry.title.map(|x| x.content),
title: entry
.title
.map(|x| str_helpers::strip_html(x.content.as_str())),
authors: entry.authors.into_iter().map(|x| x.name).collect(),
summary: entry
.summary
.map(|x| str_helpers::strip_html_tags(x.content.as_str()))
.map(|x| str_helpers::strip_html(x.content.as_str()))
.unwrap_or_default(),
url: entry
.links
Expand Down
1,482 changes: 1,482 additions & 0 deletions src/helpers/strings/lookup.rs

Large diffs are not rendered by default.

52 changes: 39 additions & 13 deletions src/helpers/strings.rs → src/helpers/strings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
mod lookup;

use regex::Regex;
use unicode_truncate::UnicodeTruncateStr;

lazy_static! {
static ref HTML_TAG_REGEX: Regex = Regex::new(r"<[^>]+>").unwrap();
/**
* Matches HTML entities in string
*
* - group 2: decimal (maybe)
* - group 3: literal (e.g. amp, gt, ...) (maybe)
*/
static ref HTML_ENTITIES_REGEX: Regex = Regex::new(r"&(#([0-9]+))?([a-z]+)?;").unwrap();
static ref REPEATED_NEWLINES_REGEX: Regex = Regex::new(r"(\r?\n|\r)\d*(\r?\n|\r)").unwrap();
}

Expand All @@ -51,18 +60,31 @@ pub fn replace_multiple_newlines(s: &str, with: &str) -> String {
REPEATED_NEWLINES_REGEX.replace_all(s, with).to_string()
}

/// strip_html_tags
/// strip_html
///
/// Strip html tags from string
pub fn strip_html_tags(s: &str) -> String {
HTML_TAG_REGEX
.replace_all(s, "")
.to_string()
.replace("&nbsp;", " ")
.replace("&amp;", "&")
.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&quot;", "\"")
/// Strip html tags and entities from string
pub fn strip_html(s: &str) -> String {
let mut escaped = HTML_TAG_REGEX.replace_all(s, "").to_string();
let copy = escaped.clone();
for group in HTML_ENTITIES_REGEX.captures_iter(copy.as_str()) {
if let Some(mtch) = group.get(2) {
// Convert mtch to u32
let replace_with = match mtch.as_str().parse::<u32>() {
Err(_) => '�',
Ok(val) => char::from_u32(val).unwrap_or('�'),
};
// Get char from decimal
escaped = escaped.replace(&group[0], replace_with.to_string().as_str());
} else if let Some(mtch) = group.get(3) {
let replace_with = lookup::HTML_ENTITIES_TABLE
.iter()
.find(|(repr, _)| *repr == mtch.as_str())
.map(|(_, code)| code)
.unwrap_or(&"�");
escaped = escaped.replace(&group[0], replace_with);
}
}
escaped
}

#[cfg(test)]
Expand Down Expand Up @@ -94,13 +116,17 @@ mod test {
}

#[test]
fn should_strip_html_tags() {
fn should_strip_html() {
assert_eq!(
strip_html_tags(
strip_html(
r#"<p><img src="https://images2.corriereobjects.it/methode_image/2021/11/09/Cultura/Foto%20Cltura%20-%20Trattate/Il%20salvataggio%20delle%20vacche%[email protected]" title="Polesine, novembre 1951,settant’anni fa l’alluvione che travolse il Veneto" alt="Polesine, novembre 1951 />Hello</p> World!"#
),
"Hello World!"
);
assert_eq!(
strip_html(r#"Hello, &lt;World&gt;&#33;"#),
"Hello, <World>!"
);
}

#[test]
Expand Down

0 comments on commit 7c6dd87

Please sign in to comment.