aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/compiler.rs161
-rw-r--r--src/lib.rs160
2 files changed, 160 insertions, 161 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 67717e5..db52837 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -11,64 +11,9 @@ use crate::util::{
skip,
span::{codes as codes_from_span, from_exit_event, serialize},
};
+use crate::{LineEnding, Options};
use std::collections::HashMap;
-/// Type of line endings in markdown.
-#[derive(Debug, Clone, PartialEq)]
-pub enum LineEnding {
- /// Both a carriage return (`\r`) and a line feed (`\n`).
- ///
- /// ## Example
- ///
- /// ```markdown
- /// a␍␊
- /// b
- /// ```
- CarriageReturnLineFeed,
- /// Sole carriage return (`\r`).
- ///
- /// ## Example
- ///
- /// ```markdown
- /// a␍
- /// b
- /// ```
- CarriageReturn,
- /// Sole line feed (`\n`).
- ///
- /// ## Example
- ///
- /// ```markdown
- /// a␊
- /// b
- /// ```
- LineFeed,
-}
-
-impl LineEnding {
- /// Turn the line ending into a [str].
- fn as_str(&self) -> &str {
- match self {
- LineEnding::CarriageReturnLineFeed => "\r\n",
- LineEnding::CarriageReturn => "\r",
- LineEnding::LineFeed => "\n",
- }
- }
- /// Turn a [Code] into a line ending.
- ///
- /// ## Panics
- ///
- /// Panics if `code` is not `\r\n`, `\r`, or `\n`.
- fn from_code(code: Code) -> LineEnding {
- match code {
- Code::CarriageReturnLineFeed => LineEnding::CarriageReturnLineFeed,
- Code::Char('\r') => LineEnding::CarriageReturn,
- Code::Char('\n') => LineEnding::LineFeed,
- _ => unreachable!("invalid code"),
- }
- }
-}
-
/// Representation of a link or image, resource or reference.
/// Reused for temporary definitions as well, in the first pass.
#[derive(Debug)]
@@ -111,110 +56,6 @@ struct Definition {
title: Option<String>,
}
-/// Configuration (optional).
-#[derive(Default, Debug)]
-pub struct Options {
- /// Whether to allow (dangerous) HTML.
- /// The default is `false`, you can turn it on to `true` for trusted
- /// content.
- ///
- /// ## Examples
- ///
- /// ```rust
- /// use micromark::{micromark, micromark_with_options, Options};
- ///
- /// // micromark is safe by default:
- /// assert_eq!(
- /// micromark("Hi, <i>venus</i>!"),
- /// "<p>Hi, &lt;i&gt;venus&lt;/i&gt;!</p>"
- /// );
- ///
- /// // Turn `allow_dangerous_html` on to allow potentially dangerous HTML:
- /// assert_eq!(
- /// micromark_with_options(
- /// "Hi, <i>venus</i>!",
- /// &Options {
- /// allow_dangerous_html: true,
- /// allow_dangerous_protocol: false,
- /// default_line_ending: None,
- ///
- /// }
- /// ),
- /// "<p>Hi, <i>venus</i>!</p>"
- /// );
- /// ```
- pub allow_dangerous_html: bool,
-
- /// Whether to allow (dangerous) protocols in links and images.
- /// The default is `false`, you can turn it on to `true` for trusted
- /// content.
- ///
- /// ## Examples
- ///
- /// ```rust
- /// use micromark::{micromark, micromark_with_options, Options};
- ///
- /// // micromark is safe by default:
- /// assert_eq!(
- /// micromark("<javascript:alert(1)>"),
- /// "<p><a href=\"\">javascript:alert(1)</a></p>"
- /// );
- ///
- /// // Turn `allow_dangerous_protocol` on to allow potentially dangerous protocols:
- /// assert_eq!(
- /// micromark_with_options(
- /// "<javascript:alert(1)>",
- /// &Options {
- /// allow_dangerous_html: false,
- /// allow_dangerous_protocol: true,
- /// default_line_ending: None,
- /// }
- /// ),
- /// "<p><a href=\"javascript:alert(1)\">javascript:alert(1)</a></p>"
- /// );
- /// ```
- pub allow_dangerous_protocol: bool,
-
- /// Default line ending to use, for line endings not in `value`.
- ///
- /// Generally, micromark copies line endings (`\r`, `\n`, `\r\n`) in the
- /// markdown document over to the compiled HTML.
- /// In some cases, such as `> a`, CommonMark requires that extra line
- /// endings are added: `<blockquote>\n<p>a</p>\n</blockquote>`.
- ///
- /// To create that line ending, the document is checked for the first line
- /// ending that is used.
- /// If there is no line ending, `default_line_ending` is used.
- /// If that isn’t configured, `\n` is used.
- ///
- /// ## Examples
- ///
- /// ```rust
- /// use micromark::{micromark, micromark_with_options, Options, LineEnding};
- ///
- /// // micromark is safe by default:
- /// assert_eq!(
- /// micromark("> a"),
- /// "<blockquote>\n<p>a</p>\n</blockquote>"
- /// );
- ///
- /// // Define `default_line_ending` to configure the default:
- /// assert_eq!(
- /// micromark_with_options(
- /// "> a",
- /// &Options {
- /// allow_dangerous_html: false,
- /// allow_dangerous_protocol: false,
- /// default_line_ending: Some(LineEnding::CarriageReturnLineFeed),
- ///
- /// }
- /// ),
- /// "<blockquote>\r\n<p>a</p>\r\n</blockquote>"
- /// );
- /// ```
- pub default_line_ending: Option<LineEnding>,
-}
-
/// Handle an event.
///
/// The current event is available at `context.events[context.index]`.
diff --git a/src/lib.rs b/src/lib.rs
index 1f49303..ff8e938 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -16,8 +16,166 @@ mod unicode;
mod util;
use crate::compiler::compile;
-pub use crate::compiler::{LineEnding, Options};
use crate::parser::parse;
+use crate::tokenizer::Code;
+
+/// Type of line endings in markdown.
+#[derive(Debug, Clone, PartialEq)]
+pub enum LineEnding {
+ /// Both a carriage return (`\r`) and a line feed (`\n`).
+ ///
+ /// ## Example
+ ///
+ /// ```markdown
+ /// a␍␊
+ /// b
+ /// ```
+ CarriageReturnLineFeed,
+ /// Sole carriage return (`\r`).
+ ///
+ /// ## Example
+ ///
+ /// ```markdown
+ /// a␍
+ /// b
+ /// ```
+ CarriageReturn,
+ /// Sole line feed (`\n`).
+ ///
+ /// ## Example
+ ///
+ /// ```markdown
+ /// a␊
+ /// b
+ /// ```
+ LineFeed,
+}
+
+impl LineEnding {
+ /// Turn the line ending into a [str].
+ fn as_str(&self) -> &str {
+ match self {
+ LineEnding::CarriageReturnLineFeed => "\r\n",
+ LineEnding::CarriageReturn => "\r",
+ LineEnding::LineFeed => "\n",
+ }
+ }
+ /// Turn a [Code] into a line ending.
+ ///
+ /// ## Panics
+ ///
+ /// Panics if `code` is not `\r\n`, `\r`, or `\n`.
+ fn from_code(code: Code) -> LineEnding {
+ match code {
+ Code::CarriageReturnLineFeed => LineEnding::CarriageReturnLineFeed,
+ Code::Char('\r') => LineEnding::CarriageReturn,
+ Code::Char('\n') => LineEnding::LineFeed,
+ _ => unreachable!("invalid code"),
+ }
+ }
+}
+
+/// Configuration (optional).
+#[derive(Default, Debug)]
+pub struct Options {
+ /// Whether to allow (dangerous) HTML.
+ /// The default is `false`, you can turn it on to `true` for trusted
+ /// content.
+ ///
+ /// ## Examples
+ ///
+ /// ```rust
+ /// use micromark::{micromark, micromark_with_options, Options};
+ ///
+ /// // micromark is safe by default:
+ /// assert_eq!(
+ /// micromark("Hi, <i>venus</i>!"),
+ /// "<p>Hi, &lt;i&gt;venus&lt;/i&gt;!</p>"
+ /// );
+ ///
+ /// // Turn `allow_dangerous_html` on to allow potentially dangerous HTML:
+ /// assert_eq!(
+ /// micromark_with_options(
+ /// "Hi, <i>venus</i>!",
+ /// &Options {
+ /// allow_dangerous_html: true,
+ /// allow_dangerous_protocol: false,
+ /// default_line_ending: None,
+ /// }
+ /// ),
+ /// "<p>Hi, <i>venus</i>!</p>"
+ /// );
+ /// ```
+ pub allow_dangerous_html: bool,
+
+ /// Whether to allow (dangerous) protocols in links and images.
+ /// The default is `false`, you can turn it on to `true` for trusted
+ /// content.
+ ///
+ /// ## Examples
+ ///
+ /// ```rust
+ /// use micromark::{micromark, micromark_with_options, Options};
+ ///
+ /// // micromark is safe by default:
+ /// assert_eq!(
+ /// micromark("<javascript:alert(1)>"),
+ /// "<p><a href=\"\">javascript:alert(1)</a></p>"
+ /// );
+ ///
+ /// // Turn `allow_dangerous_protocol` on to allow potentially dangerous protocols:
+ /// assert_eq!(
+ /// micromark_with_options(
+ /// "<javascript:alert(1)>",
+ /// &Options {
+ /// allow_dangerous_html: false,
+ /// allow_dangerous_protocol: true,
+ /// default_line_ending: None,
+ /// }
+ /// ),
+ /// "<p><a href=\"javascript:alert(1)\">javascript:alert(1)</a></p>"
+ /// );
+ /// ```
+ pub allow_dangerous_protocol: bool,
+
+ /// Default line ending to use, for line endings not in `value`.
+ ///
+ /// Generally, micromark copies line endings (`\r`, `\n`, `\r\n`) in the
+ /// markdown document over to the compiled HTML.
+ /// In some cases, such as `> a`, CommonMark requires that extra line
+ /// endings are added: `<blockquote>\n<p>a</p>\n</blockquote>`.
+ ///
+ /// To create that line ending, the document is checked for the first line
+ /// ending that is used.
+ /// If there is no line ending, `default_line_ending` is used.
+ /// If that isn’t configured, `\n` is used.
+ ///
+ /// ## Examples
+ ///
+ /// ```rust
+ /// use micromark::{micromark, micromark_with_options, Options, LineEnding};
+ ///
+ /// // micromark uses `\n` by default:
+ /// assert_eq!(
+ /// micromark("> a"),
+ /// "<blockquote>\n<p>a</p>\n</blockquote>"
+ /// );
+ ///
+ /// // Define `default_line_ending` to configure the default:
+ /// assert_eq!(
+ /// micromark_with_options(
+ /// "> a",
+ /// &Options {
+ /// allow_dangerous_html: false,
+ /// allow_dangerous_protocol: false,
+ /// default_line_ending: Some(LineEnding::CarriageReturnLineFeed),
+ /// }
+ /// ),
+ /// "<blockquote>\r\n<p>a</p>\r\n</blockquote>"
+ /// );
+ /// ```
+ pub default_line_ending: Option<LineEnding>,
+}
/// Turn markdown into HTML.
///