Skip to content

Commit

Permalink
Render image text and link with purple colour
Browse files Browse the repository at this point in the history
Make image links stand out and yet be different from regular links.

Closes GH-140
  • Loading branch information
swsnr committed Jun 22, 2020
1 parent 341708f commit d25f5fb
Show file tree
Hide file tree
Showing 70 changed files with 164 additions and 140 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ To publish a new release run `scripts/release` from the project directory.

### Changed
- `mdcat::push_tty` now returns an error if `base_dir` is a relative path.
- Image links now use purple foreground text (see [GH-140]).

### Fixed
- Always treat links targets as URLs, never as paths.

[GH-140]: https://github.com/lunaryorn/mdcat/issues/140

## [0.19.0] – 2020-06-19
### Added
- Release packages now include generated shell completions for Bash, Zsh and Fish.
Expand Down
15 changes: 14 additions & 1 deletion src/render/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use ansi_term::Colour;
use pulldown_cmark::CowStr;

#[derive(Debug, PartialEq)]
pub struct Link<'a> {
pub(crate) index: u16,
pub(crate) target: CowStr<'a>,
pub(crate) title: CowStr<'a>,
pub(crate) colour: Colour,
}

/// Data associated with rendering state.
Expand All @@ -29,13 +31,24 @@ pub struct StateData<'a> {
}

impl<'a> StateData<'a> {
pub(crate) fn add_link(mut self, target: CowStr<'a>, title: CowStr<'a>) -> (Self, u16) {
/// Add a pending link to the state data.
///
/// `target` is the link target, and `title` the link title to show after the URL.
/// `colour` is the colour to use for foreground text to differentiate between
/// different types of links.
pub(crate) fn add_link(
mut self,
target: CowStr<'a>,
title: CowStr<'a>,
colour: Colour,
) -> (Self, u16) {
let index = self.next_link;
self.next_link += 1;
self.pending_links.push(Link {
index,
target,
title,
colour,
});
(self, index)
}
Expand Down
14 changes: 11 additions & 3 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ pub fn write_event<'a, W: Write>(
(stack.pop(), data)
}
(Stacked(stack, Inline(InlineText, attrs)), End(Link(_, target, title))) => {
let (data, index) = data.add_link(target, title);
let (data, index) = data.add_link(target, title, Colour::Blue);
write_styled(
writer,
&settings.terminal_capabilities,
Expand Down Expand Up @@ -598,7 +598,15 @@ pub fn write_event<'a, W: Write>(
(ImageCapability::NoImages, _) => None,
(_, None) => None,
}
.unwrap_or_else(|| Inline(InlineText, InlineAttrs { indent, style }));
.unwrap_or_else(|| {
Inline(
InlineText,
InlineAttrs {
indent,
style: style.fg(Colour::Purple),
},
)
});
stack
.push(Inline(state, attrs))
.current(image_state)
Expand All @@ -607,7 +615,7 @@ pub fn write_event<'a, W: Write>(
(Stacked(stack, RenderedImage), Text(_)) => (Stacked(stack, RenderedImage), data),
(Stacked(stack, RenderedImage), End(Image(_, _, _))) => (stack.pop(), data),
(Stacked(stack, Inline(_, attrs)), End(Image(_, target, title))) => {
let (data, index) = data.add_link(target, title);
let (data, index) = data.add_link(target, title, Colour::Purple);
write_styled(
writer,
&settings.terminal_capabilities,
Expand Down
2 changes: 1 addition & 1 deletion src/render/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn write_link_refs<W: Write>(
if !links.is_empty() {
writeln!(writer)?;
for link in links {
let style = Style::new().fg(Colour::Blue);
let style = Style::new().fg(link.colour);
let link_text = format!("[{}]: {} {}", link.index, link.target, link.title);
write_styled(writer, capabilities, &style, link_text)?;
writeln!(writer)?;
Expand Down
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/513-links
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
moon[1][2]
moon[1][2]

[1]: moon.jpg 
[1]: moon.jpg 
[2]: /uri 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/516-links
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[foo[1]](uri2)[2]
[foo[1]](uri2)[2]

[1]: uri1 
[2]: uri3 
[2]: uri3 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/527-links
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
moon[1][2]
moon[1][2]

[1]: moon.jpg 
[1]: moon.jpg 
[2]: /uri 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/568-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/569-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: train.jpg train & tracks
[1]: train.jpg train & tracks
6 changes: 3 additions & 3 deletions tests/render/golden/ansi-only/commonmark-spec/570-images
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo bar[1][2]
foo bar[1][2]

[1]: /url 
[2]: /url2 
[1]: /url 
[2]: /url2 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/571-images
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo bar[1][2]
foo bar[1][2]

[1]: /url 
[2]: /url2 
[2]: /url2 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/572-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: train.jpg train & tracks
[1]: train.jpg train & tracks
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/573-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: train.jpg train & tracks
[1]: train.jpg train & tracks
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/574-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: train.jpg 
[1]: train.jpg 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/575-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
My foo bar[1]
My foo bar[1]

[1]: /path/to/train.jpg title
[1]: /path/to/train.jpg title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/576-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: url 
[1]: url 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/577-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[1]
[1]

[1]: /url 
[1]: /url 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/578-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url 
[1]: /url 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/579-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url 
[1]: /url 
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/580-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/581-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/582-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Foo[1]
Foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/583-images
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo[1]
foo[1]
[]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/584-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/585-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/ansi-only/commonmark-spec/587-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Foo[1]
Foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/513-links
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;

[1]: moon.jpg 
[1]: moon.jpg 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/516-links
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[]8;;file://HOSTNAME/WORKING_DIRECTORY/tests/render/md/commonmark-spec/uri1foo]8;;](uri2)[1]
[]8;;file://HOSTNAME/WORKING_DIRECTORY/tests/render/md/commonmark-spec/uri1foo]8;;](uri2)[1]

[1]: uri3 
[1]: uri3 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/527-links
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;

[1]: moon.jpg 
[1]: moon.jpg 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/568-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/569-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: train.jpg train & tracks
[1]: train.jpg train & tracks
6 changes: 3 additions & 3 deletions tests/render/golden/iterm2/commonmark-spec/570-images
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo bar[1][2]
foo bar[1][2]

[1]: /url 
[2]: /url2 
[1]: /url 
[2]: /url2 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/571-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo ]8;;file://HOSTNAME/ROOT/urlbar]8;;[1]
foo ]8;;file://HOSTNAME/ROOT/urlbar]8;;[1]

[1]: /url2 
[1]: /url2 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/572-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: train.jpg train & tracks
[1]: train.jpg train & tracks
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/573-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: train.jpg train & tracks
[1]: train.jpg train & tracks
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/574-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: train.jpg 
[1]: train.jpg 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/575-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
My foo bar[1]
My foo bar[1]

[1]: /path/to/train.jpg title
[1]: /path/to/train.jpg title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/576-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: url 
[1]: url 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/577-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[1]
[1]

[1]: /url 
[1]: /url 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/578-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url 
[1]: /url 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/579-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url 
[1]: /url 
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/580-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/581-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/582-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Foo[1]
Foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/583-images
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo[1]
foo[1]
[]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/584-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/585-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo bar[1]
foo bar[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/iterm2/commonmark-spec/587-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Foo[1]
Foo[1]

[1]: /url title
[1]: /url title
4 changes: 2 additions & 2 deletions tests/render/golden/vte50/commonmark-spec/513-links
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;

[1]: moon.jpg 
[1]: moon.jpg 
4 changes: 2 additions & 2 deletions tests/render/golden/vte50/commonmark-spec/516-links
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[]8;;file://HOSTNAME/WORKING_DIRECTORY/tests/render/md/commonmark-spec/uri1foo]8;;](uri2)[1]
[]8;;file://HOSTNAME/WORKING_DIRECTORY/tests/render/md/commonmark-spec/uri1foo]8;;](uri2)[1]

[1]: uri3 
[1]: uri3 
4 changes: 2 additions & 2 deletions tests/render/golden/vte50/commonmark-spec/527-links
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;
]8;;file://HOSTNAME/ROOT/urimoon[1]]8;;

[1]: moon.jpg 
[1]: moon.jpg 
4 changes: 2 additions & 2 deletions tests/render/golden/vte50/commonmark-spec/568-images
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo[1]
foo[1]

[1]: /url title
[1]: /url title
Loading

0 comments on commit d25f5fb

Please sign in to comment.