/src/lib.rs

https://github.com/robinst/linkify · Rust · 120 lines · 15 code · 4 blank · 101 comment · 0 complexity · 641a090d94d7fe27f6317e468a441379 MD5 · raw file

  1. //! Linkify finds links such as URLs and email addresses in plain text.
  2. //! It's smart about where a link ends, such as with trailing punctuation.
  3. //!
  4. //! Your reaction might be: "Do I need a library for this? Why not a regex?".
  5. //! Let's look at a few cases:
  6. //!
  7. //! * In `http://example.com/.` the link should not include the trailing dot
  8. //! * `http://example.com/,` should not include the trailing comma
  9. //! * `(http://example.com/)` should not include the parens
  10. //!
  11. //! Seems simple enough. But then we also have these cases:
  12. //!
  13. //! * `https://en.wikipedia.org/wiki/Link_(The_Legend_of_Zelda)` should include the trailing paren
  14. //! * `http://üñîçøðé.com/ä` should also work for Unicode (including Emoji and Punycode)
  15. //! * `<http://example.com/>` should not include angle brackets
  16. //!
  17. //! This library behaves as you'd expect in the above cases and many more.
  18. //! It uses a simple scan with linear runtime.
  19. //!
  20. //! In addition to URLs, it can also find emails.
  21. //!
  22. //! ### Usage
  23. //!
  24. //! Basic usage:
  25. //!
  26. //! ```
  27. //! use linkify::{LinkFinder, LinkKind};
  28. //!
  29. //! let input = "Have you seen http://example.com?";
  30. //! let finder = LinkFinder::new();
  31. //! let links: Vec<_> = finder.links(input).collect();
  32. //!
  33. //! assert_eq!(1, links.len());
  34. //! let link = &links[0];
  35. //!
  36. //! assert_eq!("http://example.com", link.as_str());
  37. //! assert_eq!(14, link.start());
  38. //! assert_eq!(32, link.end());
  39. //! assert_eq!(&LinkKind::Url, link.kind());
  40. //! ```
  41. //!
  42. //! Restrict the kinds of links:
  43. //!
  44. //! ```
  45. //! use linkify::{LinkFinder, LinkKind};
  46. //!
  47. //! let input = "http://example.com and foo@example.com";
  48. //! let mut finder = LinkFinder::new();
  49. //! finder.kinds(&[LinkKind::Email]);
  50. //! let links: Vec<_> = finder.links(input).collect();
  51. //!
  52. //! assert_eq!(1, links.len());
  53. //! let link = &links[0];
  54. //! assert_eq!("foo@example.com", link.as_str());
  55. //! assert_eq!(&LinkKind::Email, link.kind());
  56. //! ```
  57. //!
  58. //! Split the text into consecutive spans (mixed links and plain text).
  59. //!
  60. //! ```
  61. //! use linkify::{LinkFinder, LinkKind};
  62. //!
  63. //! let input = "Have you seen http://example.com?";
  64. //! let finder = LinkFinder::new();
  65. //! let spans: Vec<_> = finder.spans(input).collect();
  66. //!
  67. //! assert_eq!(3, spans.len());
  68. //!
  69. //! assert_eq!("Have you seen ", spans[0].as_str());
  70. //! assert_eq!(0, spans[0].start());
  71. //! assert_eq!(14, spans[0].end());
  72. //! assert_eq!(None, spans[0].kind());
  73. //!
  74. //! assert_eq!("http://example.com", spans[1].as_str());
  75. //! assert_eq!(14, spans[1].start());
  76. //! assert_eq!(32, spans[1].end());
  77. //! assert_eq!(Some(&LinkKind::Url), spans[1].kind());
  78. //!
  79. //! assert_eq!("?", spans[2].as_str());
  80. //! assert_eq!(32, spans[2].start());
  81. //! assert_eq!(33, spans[2].end());
  82. //! assert_eq!(None, spans[2].kind());
  83. //! ```
  84. //!
  85. //! ### Conformance
  86. //!
  87. //! This crates makes an effort to respect the various standards, namely:
  88. //!
  89. //! * [RFC 3986] and [RFC 3987] for URLs
  90. //! * [RFC 5321] and [RFC 6531] for emails (except IP addresses and quoting)
  91. //!
  92. //! At the same time, it does not guarantee that the returned links are valid.
  93. //! If in doubt, it rather returns a link than skipping it.
  94. //!
  95. //! If you need to validate URLs, e.g. for checking TLDs, use another library on
  96. //! the returned links.
  97. //!
  98. //! [RFC 3986]: https://tools.ietf.org/search/rfc3986
  99. //! [RFC 3987]: https://tools.ietf.org/search/rfc3987
  100. //! [RFC 5321]: https://tools.ietf.org/search/rfc5321
  101. //! [RFC 6531]: https://tools.ietf.org/search/rfc6531
  102. #![doc(html_root_url = "https://docs.rs/linkify/0.7.0")]
  103. #![deny(warnings)]
  104. #![deny(missing_docs)]
  105. #![deny(missing_debug_implementations)]
  106. mod email;
  107. mod finder;
  108. mod scanner;
  109. mod url;
  110. pub use crate::finder::Link;
  111. pub use crate::finder::LinkFinder;
  112. pub use crate::finder::LinkKind;
  113. pub use crate::finder::Links;
  114. pub use crate::finder::{Span, Spans};
  115. #[cfg(doctest)]
  116. doc_comment::doctest!("../README.md");