6 releases

0.2.2 Feb 20, 2023
0.2.1 Oct 1, 2022
0.2.0 Sep 19, 2022
0.2.0-alpha.2 May 2, 2022
0.1.0 Dec 20, 2021

#258 in Web programming

Download history 64/week @ 2022-12-02 51/week @ 2022-12-09 27/week @ 2022-12-16 23/week @ 2022-12-23 26/week @ 2022-12-30 149/week @ 2023-01-06 91/week @ 2023-01-13 179/week @ 2023-01-20 282/week @ 2023-01-27 215/week @ 2023-02-03 62/week @ 2023-02-10 89/week @ 2023-02-17 150/week @ 2023-02-24 140/week @ 2023-03-03 121/week @ 2023-03-10 120/week @ 2023-03-17

539 downloads per month
Used in 3 crates

GPL-3.0-or-later

105KB
2K SLoC

mwtitle

crates.io pipeline status coverage report

The mwtitle crate is a library for parsing, normalizing and formatting MediaWiki page titles. See the documentation (main) for more details.

License

mwtitle is released under the GPL v3 or any later version. It contains code directly copied and ported from MediaWiki by various authors. The Rust parts were primarily written by Erutuon and Kunal Mehta.


lib.rs:

mwtitle

mwtitle is a library for parsing, normalizing and formatting MediaWiki page titles. It is primarily a port of the MediaWikiTitleCodec class from MediaWiki, and passes the MediaWiki test suite.

The easiest way to get started is create a [TitleCodec] from a siteinfo API request.

# #[tokio::main]
# async fn main() -> anyhow::Result<()> {
# #[cfg(feature = "parsing")]
# {
# use mwtitle::{SiteInfoResponse, TitleCodec};
let url = "https://en.wikipedia.org/w/api.php\
           ?action=query&meta=siteinfo\
           &siprop=general|namespaces|namespacealiases|interwikimap\
           &formatversion=2&format=json";
let resp: SiteInfoResponse = reqwest::get(url).await?.json().await?;
let codec = TitleCodec::from_site_info(resp.query)?;
let title = codec.new_title("Talk:Main Page#Section 1")?;
assert_eq!(title.namespace(), 1);
assert_eq!(title.dbkey(), "Main_Page");
assert_eq!(title.fragment(), Some("Section 1"));
assert_eq!(codec.to_pretty(&title), "Talk:Main Page".to_string());
assert_eq!(
    codec.to_pretty_with_fragment(&title),
    "Talk:Main Page#Section 1".to_string()
);
# }
# Ok(())
# }

It's also possible to possible to create a TitleCodec from a JSON siteinfo-namespaces.json or compressed siteinfo-namespaces.json.gz that comes from Wikimedia dumps. This requires the extra utils feature to be enabled.

Contributing

mwtitle is a part of the mwbot-rs project. We're always looking for new contributors, please reach out if you're interested!

Dependencies

~1–2MB
~45K SLoC