Replacing image placeholders with actual paths

Hi,

Sigh. I have to admit to be totally unable to create the necessary replacement formula. None of the examples in this forum seem to exactly match my case. RegEx user guides are not helping my headache.

What I’m trying to do is replacing the shorthand image placeholders I put in my text, with actual paths and names. The placeholders are something like this:

<img-sw my-screenshot.png>

This means that the file is named “my-screenshot.png”, and it is an image (“img”) related to software (“sw”).

I would like the image to be found in an “image” folder placed next to the folder where I keep my Scrivener project:

  • project_folder/scrivener_project.scriv
  • images/

I would like the image to have an {.image-sw} class attached, to allow eventual manipulations different from those of {.icon} or {.image-hw} images.

I’ve tried to use this replacement formula in the Compile dialog:

Replace:
<img-sw-\w>

With:
![](../../../images/\w){.image-sw}

The number of up commands (…/) is still arbitrary, and is just relative to the folder where .md files are saved for Quarto.

Should I say that the above formula is not working at all? What am I doing wrong?

Paolo

Better use the Replacements option in the Compile Format Designer. Most Formats actually have good examples, so you can see what’s expected.
This worked as a charm for my book, that had dozens of images only included at compile time.
I did include the exact file path on my machine, but used different subfolders per chapter in the Placeholder.

P.S. Is the code in your post complete? Seems like some characters are missing. Use backslashes to escape alternative interpretations or the “Code”-tag to insert code…

Yep, it’s what I’m trying to use.

I don’t know. I’m trying to create a RegEx formula trying to find a logic in it. The only escaped symbol I used is ‘\w’, that in my intention is “all characters memorized from the string to be found, reissued in the replaced string”. I’m hoping that this:

<img-sw-my-screenshot.png>

becomes this:

![](../../../images/my-screenshot.png){.image-sw}

It doesn’t. :frowning:

Paolo

Try this pair:

Replace: <img-sw-([^>]+)>
With: ![](../../../images/$1){.image-sw}

That part in parentheses means: find one or more of anything except >, and store it for later. The $1 in the replacement prints what was stored in that place. If you don’t put a parentheses around something then nothing gets stored.

Do be careful of spaces in file names though, if your example was meant to show how a filename might look. Spaces aren’t the easiest to use and usually need to be turned into %20, which isn’t easy to do all at once with a single replacement. It would be easier to bulk rename from “sample image.jpg” to “sample_image.jpg” in my opinion.

1 Like

Thank you, Ioa. Really more complicate than I could imagine, but I sort of get how your formula works. Let’s try it.

Paolo