summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-09-08 16:26:37 +0000
committerLars Wirzenius <liw@liw.fi>2022-09-08 16:26:37 +0000
commitac76338e0ca003f1da6ba54060140381e48ebbc9 (patch)
tree4958684fe22a511d8a764d20e30575a1ab39d5dc
parentc6cb630275ef9edd8a9818a4883f2a2b59b4aa46 (diff)
parenta989d141eff5887ba46f65c217a0175a6ad53e1d (diff)
downloadriki-ac76338e0ca003f1da6ba54060140381e48ebbc9.tar.gz
Merge branch 'img-link-gen' into 'main'
feat: img directive link generation See merge request larswirzenius/riki!52
-rw-r--r--riki.md41
-rw-r--r--src/directive/img.rs32
2 files changed, 66 insertions, 7 deletions
diff --git a/riki.md b/riki.md
index 988777b..199130e 100644
--- a/riki.md
+++ b/riki.md
@@ -447,7 +447,7 @@ then file output/index.html contains "<img src="img.jpg""
~~~
~~~{#img .file .markdown}
-[[!img img.jpg]]]
+[[!img img.jpg]]
~~~
~~~{#jpeg .file}
@@ -477,9 +477,9 @@ then file output/index.html contains "<img src="c.jpg" height="200">"
~~~
~~~{#img-size .file .markdown}
-[[!img a.jpg size="100x200"]]]
-[[!img b.jpg size="100x"]]]
-[[!img c.jpg size="x200"]]]
+[[!img a.jpg size="100x200"]]
+[[!img b.jpg size="100x"]]
+[[!img c.jpg size="x200"]]
~~~
#### Image attributes
@@ -511,9 +511,40 @@ then file output/index.html contains "vspace="vspc""
~~~{#img-attr .file .markdown}
[[!img img.jpg alt="halt malt" title="tightle" class="klass" align="malign"
- id="kid" hspace="hspc" vspace="vspc"]]]
+ id="kid" hspace="hspc" vspace="vspc"]]
~~~
+#### Image link generation
+
+_Requirement: the `img` directive can make an image be a link._
+
+The [ikiwiki img
+directive](http://ikiwiki.info/ikiwiki/directive/img/) allows
+arguments:
+
+> The link parameter is used to control whether the scaled image links
+> to the full size version. By default it does; set "link=somepage" to
+> link to another page instead, or "link=no" to disable the link, or
+> "link=http://url" to link to a given url.
+
+~~~scenario
+given an installed riki
+given file site/index.mdwn from img-link
+given file site/a.jpg from jpeg
+given file site/b.jpg from jpeg
+when I run riki build site output
+when I run cat output/index.html
+then file output/index.html contains "<a href="a.jpg"><img src="a.jpg""
+then file output/index.html doesn't contain "<a href="b.jpg"><img src="b.jpg""
+~~~
+
+~~~{#img-link .file .markdown}
+[[!img a.jpg]]
+[[!img b.jpg link=no]]
+~~~
+
+
+
### `meta title`
_Requirement: the `meta title` directive sets page title._
diff --git a/src/directive/img.rs b/src/directive/img.rs
index 0e6cc79..3ee6501 100644
--- a/src/directive/img.rs
+++ b/src/directive/img.rs
@@ -9,6 +9,7 @@ use std::path::Path;
#[derive(Debug, Eq, PartialEq)]
pub struct Img {
src: String,
+ link: bool,
align: Option<String>,
alt: Option<String>,
class: Option<String>,
@@ -23,13 +24,14 @@ pub struct Img {
impl Img {
pub const REQUIRED: &'static [&'static str] = &[];
pub const ALLOWED: &'static [&'static str] = &[
- "align", "alt", "class", "hspace", "id", "size", "title", "vspace",
+ "align", "alt", "class", "hspace", "id", "link", "size", "title", "vspace",
];
pub const ALLOW_ANY_UNNAMED: bool = true;
fn new(src: String) -> Self {
Self {
src,
+ link: true,
align: None,
alt: None,
class: None,
@@ -42,6 +44,10 @@ impl Img {
}
}
+ fn link(&mut self, link: bool) {
+ self.link = link;
+ }
+
fn align(&mut self, align: String) {
self.align = Some(align);
}
@@ -86,7 +92,18 @@ impl Img {
);
let src = site.resolve(meta.path(), Path::new(&self.src))?;
trace!("img src={:?}", src.display());
- let mut img = format!("<img src=\"{}\"", self.src);
+
+ let mut img = String::new();
+ let src = Some(self.src.clone());
+
+ if self.link {
+ img.push_str("<a");
+ push_attr(&mut img, "href", &src);
+ img.push('>');
+ }
+
+ img.push_str("<img");
+ push_attr(&mut img, "src", &src);
if let Some(w) = self.width {
img.push_str(&format!(" width=\"{}\"", w));
}
@@ -101,6 +118,11 @@ impl Img {
push_attr(&mut img, "title", &self.title);
push_attr(&mut img, "vspace", &self.vspace);
img.push('>');
+
+ if self.link {
+ img.push_str("</a>");
+ }
+
Ok(img)
}
}
@@ -121,6 +143,12 @@ impl From<&ParsedDirective> for Img {
let mut img = Img::new(unnamed.into());
let args = p.args();
+ if let Some(link) = args.get("link") {
+ if *link == "no" {
+ img.link(false);
+ }
+ }
+
if let Some(size) = args.get("size") {
if let Some((w, h)) = size.split_once('x') {
if let Ok(w) = w.parse() {