RaspberryPiFoundation / lesson_format

Lesson formatter
17 stars 28 forks source link

Adding a project Images folder #166

Closed rikcross closed 8 years ago

rikcross commented 8 years ago

Currently, a project's images are just in the root folder of the project, which looks quite messy.

screen shot 2015-07-30 at 15 39 02

It'd be great to add an Images folder, to tidy things up, like this:

screen shot 2015-07-30 at 09 47 08

Adding in the folder is no problem, but in the project's .md file notes, I'll need to edit every instance of:

![screenshot](imageName.png)

into

![screenshot](./Images/imageName.png)

Is there a quick way of making these changes. I don't want to do it manually, because there are a lot.

I've put together what I think is a regular expression to search for images:

!\[[A-Za-z]*\]\([A-Za-z]*.png\)

Can anyone help with writing a shell script to loop through all images in all files and update the links?

Thanks.

arve0 commented 8 years ago

While I do not agree with it being messy (first implementation of find was implemented in 1971, file managers sort by time, type, name, etc), you can fix the "mess" with sed and Python.

Links can be altered with a sed script like this: https://github.com/arve0/codeclub_lesson_builder/blob/master/utils/fix_code_blocks.sh

Moving images after patterns can be done with the Python modules glob and shutils.

andylolz commented 8 years ago

tbh I pretty much always use Sublime Text’s “Find in Files…” for this sort of thing.

I think your regex currently misses instances with spaces in the alt text, for instance, or hyphens in the filename. You can perhaps instead search for something really general, like: !\[(.*)\]\((.+)\) (explanation here.)

And replace (in Sublime Text) with something like: ![\1](images/\2)

Yeah – agree with @arve0 on the second bit – glob and shutils is also how I’d move the files about (which is the harder task, really).

rikcross commented 8 years ago

Thanks guys!