Skip to content

Conversation

@stepantubanov
Copy link
Contributor

@stepantubanov stepantubanov commented Mar 25, 2024

Previous attempt:

In this version I've just added an associated type on StrWrite so that io::Error isn't lost. Let me know if that makes sense.

Other alternatives: Instead of exposing StrWrite, IoWriter and FmtWriter we can expose two functions: write_html_io and write_html_fmt that accept io::Write and fmt::Write (push_html can be removed then).

NOTE. This is a breaking change (should this PR bump version?) Changed base to 0.11

@Martin1887
Copy link
Collaborator

This must be reviewed and analyzed, but yes, it is a breaking change, and yes, it means an increment of the version number and indeed it is planned for the 0.11 milestone (#492).

Also, this pull request should be created against the branch 'branch_0.11', that is the branch where all breaking changes for the 0.11 version are being merged.

Thanks!

@stepantubanov stepantubanov changed the base branch from master to branch_0.11 March 26, 2024 13:59
@Martin1887 Martin1887 linked an issue Mar 30, 2024 that may be closed by this pull request
@Martin1887
Copy link
Collaborator

Martin1887 commented Apr 6, 2024

Interesting approach, it seems the only inconvenience is having to manually create a IoWrapper or a FmtWrapper...

And I think it could be solved implementing StrWrite for fmt::Write and io::Write. Also, the build seemed to fail.

@Martin1887 Martin1887 requested a review from notriddle April 6, 2024 09:48
@stepantubanov
Copy link
Contributor Author

stepantubanov commented Apr 12, 2024

And I think it could be solved implementing StrWrite for fmt::Write and io::Write

Do you mean impl<T: fmt::Write> StrWrite for T and impl<T: io::Write> StrWrite for T? That doesn't work since these impls are conflicting.

Also, the build seemed to fail.

Fixed and rebased.

@Martin1887
Copy link
Collaborator

I see, so this seems OK for me, but I would like considering other options and the opinion from other reviewers before merging it.

Thanks!

@Martin1887 Martin1887 requested a review from ollpu April 14, 2024 09:03
Copy link
Collaborator

@ollpu ollpu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API-wise I definitely prefer the idea of just having separate entry functions write_html_io/fmt. Having to use the name write_html_fmt for strings (the most common case) is a little obtuse. Maybe we could keep push_html anyway. Although it is technically redundant, the name is a bit clearer and keeping it would reduce breakage.

Internally using a trait with an associated error type for writing looks good to me.

Co-authored-by: Roope Salmi <rpsalmi@gmail.com>
@notriddle
Copy link
Collaborator

I agree with @ollpu. Less complex API with less surface area is better.

Copy link
Collaborator

@ollpu ollpu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

It would be even better if we didn't have to expose StrWrite and the wrappers in pulldown-cmark-escape. I guess one way to do it would be to have structs that impl Display, e.g. EscapeHtml, and always using write!() or similar. That's something for a later PR though.

@Martin1887 Martin1887 merged commit 7a1e53c into pulldown-cmark:branch_0.11 Apr 17, 2024
@Martin1887
Copy link
Collaborator

Perfect, merge! Adding the EscapeHtml and co structs seems a good point for the 0.11 release, so I think they should be added before releasing it.

The other remaining issue for the 0.11 is #67.

@Martin1887
Copy link
Collaborator

I'm trying to hide StrWriter and the associated structs but I'm not getting it without duplicating the code for the methods of HtmlWriter, since the user has to specify the type, @ollpu.

Respect to the EscapedHtml and so structs implementing Display, they are easy to implement but they would have to write in an empty String buffer, and then write that String into the writer of HtmlWriter, so it would have a performance penalty. Please correct me if I'm wrong.

My implementation for those structs (example only with EscapedHtml but EscapedHref and EscapedHtmlBodyText are identical):

pub struct EscapedHtml<'a> {
    html: &'a str,
}
impl<'a> fmt::Display for EscapedHtml<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut buffer = String::new();
        escape_html(&mut buffer, self.html)?;
        write!(f, "{}", buffer)
    }
}

@ollpu
Copy link
Collaborator

ollpu commented Apr 21, 2024

@Martin1887

On the first point, to be able to hide StrWrite from public API, it needs to be moved (or duplicated) to the pulldown-cmark crate. It should be fine for HtmlWriter to use it, since it isn't public. Tell me if I'm missing something here. I can try doing this myself later too.

For the struct Display approach, you can do this with the current StrWrite and FmtWriter:

pub struct EscapedHtml<'a> {
    html: &'a str,
}
impl<'a> fmt::Display for EscapedHtml<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        escape_html(FmtWriter(f), self.html)
    }
}

The internal functions could just take &mut fmt::Formatter directly, or a simpler trait, instead of StrWrite, though.

@Martin1887
Copy link
Collaborator

I would leave as public the trait and the structs in the pulldown_cmark_escape crate, since it is more low-level, instead of duplicating them in pulldown_cmark.

But this approach would not avoid the use of FmtWriter and IoWriter in the write_html function, right? Or at least it could not be run over an io::Write struct. Also, I think using the EscapedHtml and co structs in this function would have a performance penalty due to the double write in a temporal FmtWriter in the format call and then another time in the internal writer field (the only one done right now).

But I'm probably missing something...

@stepantubanov
Copy link
Contributor Author

@Martin1887

Also, I think using the EscapedHtml and co structs in this function would have a performance penalty due to the double write in a temporal FmtWriter in the format call and then another time in the internal writer field (the only one done right now)

Hm, these simple forwarding calls almost certainly should be zero cost and either get inlined to where they're called or inline internal writer call into them (whatever compiler decides based on heuristics).

@ollpu
Copy link
Collaborator

ollpu commented Apr 21, 2024

@Martin1887 If the escaping works through Display impls, then it can be used with either fmt or io writers by using write!(writer, "{}", EscapedHtml(x)) (it'll go through write_fmt in StrWrite).

There's definitely more for the optimizer to chew on, and I'm also vary of having extra layers of write! (I hear they don't always optimize away). If you don't think it's worth it, let's keep it as is. I don't think we should have the Display structs if we don't use them ourselves.

And yes, something similar to StrWrite is needed in pulldown-cmark/src/html.rs either way to abstract over io/fmt.

@Martin1887
Copy link
Collaborator

I think it is worthy for an easier interface to escape HTML strings, and we could even using them in the html.rs functions if the performance penalty is low, but I couldn't see how them could avoid the exposure of StrWrite, FmtWriter and IoWriter 😄 .

Could you create the pull request? Thanks!

maxdeviant referenced this pull request in zed-industries/zed Sep 5, 2024
This PR upgrades `pulldown_cmark` to v0.12.

There were a few breaking changes that needed to be accounted for:

- The `BlockQuote` variant now has a `kind` attached. Right now we're
ignoring it.
- `pulldown_cmark` now emits tags for definition lists. This codepath
has been left unimplemented, for now.

### Release Notes

<details>
<summary>raphlinus/pulldown-cmark (pulldown-cmark)</summary>

###
[`v0.12.1`](https://redirect.github.com/pulldown-cmark/pulldown-cmark/releases/tag/v0.12.1):
0.12.1

[Compare
Source](https://redirect.github.com/raphlinus/pulldown-cmark/compare/v0.12.0...v0.12.1)

##### Security

- Fix O(n\*\*2) comment parser by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/941](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/941)

##### New features

- impl From<CowStr> for String by
[@&#8203;oconnor663](https://redirect.github.com/oconnor663) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/943](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/943)

##### Developers

- Make dos-fuzzer part of the workspace by
[@&#8203;kdarkhan](https://redirect.github.com/kdarkhan) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/945](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/945)

##### New Contributors

- [@&#8203;oconnor663](https://redirect.github.com/oconnor663) made
their first contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/943](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/943)
- [@&#8203;kdarkhan](https://redirect.github.com/kdarkhan) made their
first contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/945](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/945)

**Full Changelog**:
pulldown-cmark/pulldown-cmark@v0.12.0...v0.12.1

###
[`v0.12.0`](https://redirect.github.com/pulldown-cmark/pulldown-cmark/releases/tag/v0.12.0):
0.12.0

[Compare
Source](https://redirect.github.com/raphlinus/pulldown-cmark/compare/v0.11.3...v0.12.0)

Thanks to all contributors! This release mainly adds the long awaited
commonmark-hs description lists (under a flag) and enables the
blockquote kind in `TagEnd` reverted in 0.11.2.

#### Breaking changes

- feat: re-add kind for BlockQuote in TagEnd by
[@&#8203;Martin1887](https://redirect.github.com/Martin1887) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/940](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/940)
- Refactor TextMergeStream by
[@&#8203;ollpu](https://redirect.github.com/ollpu) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/931](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/931)

#### New features

- Implement commonmark-hs compatible definition lists by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/915](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/915)

#### Other changes

- Rename superlinear time fuzzer to `dos-fuzzer` by
[@&#8203;ollpu](https://redirect.github.com/ollpu) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/938](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/938)

**Full Changelog**:
pulldown-cmark/pulldown-cmark@v0.11.2...v0.12.0

###
[`v0.11.3`](https://redirect.github.com/pulldown-cmark/pulldown-cmark/releases/tag/v0.11.3):
0.11.3

[Compare
Source](https://redirect.github.com/raphlinus/pulldown-cmark/compare/v0.11.2...v0.11.3)

#### Security

- Fix O(n\*\*2) comment parser by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/944](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/944)

**Full Changelog**:
pulldown-cmark/pulldown-cmark@v0.11.2...v0.11.3

###
[`v0.11.2`](https://redirect.github.com/pulldown-cmark/pulldown-cmark/releases/tag/v0.11.2)

[Compare
Source](https://redirect.github.com/raphlinus/pulldown-cmark/compare/v0.11.1...v0.11.2)

Revert BlockQuote kind to avoid breaking change.

###
[`v0.11.1`](https://redirect.github.com/pulldown-cmark/pulldown-cmark/releases/tag/v0.11.1)

[Compare
Source](https://redirect.github.com/raphlinus/pulldown-cmark/compare/v0.11.0...v0.11.1)

Thanks to all people involved in this release! The main change of this
release is the reduction of the MSRV to 1.71.1, but it also includes a
lot of bug fixes and a new mdBook for user-friendly documentation.

#### Breaking changes

- Add BlockQuoteKind to BlockQuote TagEnd by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/926](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/926)
(reverted in v0.11.2)

#### What's Changed

- fix: CowStr deserialization when escaping by
[@&#8203;aatifsyed](https://redirect.github.com/aatifsyed) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/895](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/895)
- fix(test): fix generating spec tests doesn't work on Windows due to
line-endings by [@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/903](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/903)
- feat: add `-G` CLI option to enable GFM support by
[@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/905](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/905)
- feat: set `DefaultBrokenLinkCallback` as the default broken link
callback of `OffsetIter` by
[@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/901](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/901)
- chore(doc): use `cargo add` to instruct how to install this crate as
dependency by [@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/904](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/904)
- fix typo by [@&#8203;jmbhughes](https://redirect.github.com/jmbhughes)
in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/909](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/909)
- Fix parsing blocks inside alert body by
[@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/908](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/908)
- fuzz: fix building fuzzer and improve fuzzing coverage by enabling
more parse options by [@&#8203;rhysd](https://redirect.github.com/rhysd)
in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/910](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/910)
- fix: fix warnings reported from nightly rustc by
[@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/911](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/911)
- fix: fix infinite loop when metadata delimiter is indented by
[@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/913](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/913)
- Raise the link cutoff from 5 to 32 by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/917](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/917)
- Reduce MSRV to 1.71.1 by separating benchmarks into a new crate with
CI enhancements by [@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/916](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/916)
- Add guide book and deploy script for it by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/883](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/883)
- ci: fix deploying the document to GitHub Pages and make the deploy job
faster by [@&#8203;rhysd](https://redirect.github.com/rhysd) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/920](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/920)
- Fix lone task list item bug by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/924](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/924)
- Fix offset range around footnotes that look like images by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/925](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/925)
- Update old footnote format to interrupt paragraph by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/928](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/928)
- Fix confusing bug with back-to-back footnotes by
[@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/930](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/930)
- Add reproduction cases for
[#&#8203;927](https://redirect.github.com/raphlinus/pulldown-cmark/issues/927)
by [@&#8203;zoni](https://redirect.github.com/zoni) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/929](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/929)
- Add regression test for
[#&#8203;655](https://redirect.github.com/raphlinus/pulldown-cmark/issues/655)
by [@&#8203;ollpu](https://redirect.github.com/ollpu) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/932](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/932)
- Renovate the superlinear time fuzzer by
[@&#8203;ollpu](https://redirect.github.com/ollpu) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/935](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/935)

#### New Contributors

- [@&#8203;aatifsyed](https://redirect.github.com/aatifsyed) made their
first contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/895](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/895)
- [@&#8203;jmbhughes](https://redirect.github.com/jmbhughes) made their
first contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/909](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/909)
- [@&#8203;zoni](https://redirect.github.com/zoni) made their first
contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/929](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/929)

**Full Changelog**:
pulldown-cmark/pulldown-cmark@v0.11.0...v0.11.1

###
[`v0.11.0`](https://redirect.github.com/pulldown-cmark/pulldown-cmark/releases/tag/v0.11.0):
0.11.0

[Compare
Source](https://redirect.github.com/raphlinus/pulldown-cmark/compare/v0.10.3...v0.11.0)

##### Finally, the so long awaited math mode is here! Enable the option
to use it.

This release also includes other improvements and bugfixes, please see
the changelog below for more details. Thanks to all contributors that
has made possible this release!

#### Breaking changes

- Change `write_to_html` to allow `fmt::Write` by
[@&#8203;stepantubanov](https://redirect.github.com/stepantubanov) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/870](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/870)

#### New features

-   Math mode

#### Bugfixes

- \[0.11] Don't exit `scan_attribute` with the ix pointing at block
quote by [@&#8203;notriddle](https://redirect.github.com/notriddle) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/873](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/873)
- (Re)introduce simd feature to pulldown-cmark-escape by
[@&#8203;ollpu](https://redirect.github.com/ollpu) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/880](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/880)
- fix: remove unnecessary end_newline set by
[@&#8203;tomcur](https://redirect.github.com/tomcur) in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/885](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/885)

#### New Contributors

- [@&#8203;duskmoon314](https://redirect.github.com/duskmoon314) made
their first contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/874](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/874)
- [@&#8203;stepantubanov](https://redirect.github.com/stepantubanov)
made their first contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/870](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/870)
- [@&#8203;tomcur](https://redirect.github.com/tomcur) made their first
contribution in
[https://github.com/pulldown-cmark/pulldown-cmark/pull/885](https://redirect.github.com/pulldown-cmark/pulldown-cmark/pull/885)

**Full Changelog**:
pulldown-cmark/pulldown-cmark@v0.10.3...v0.11.0

</details>

Release Notes:

- N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use core::fmt::Write instead of custom StrWrite?

4 participants