Add custom rename function #38

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants
@hellosmithy

Currently Grunt's built in rename function requires a dest option to be specified to run and is not compatible with the custom_destoption provided by this plugin. As it is useful to be able to run replace functions I have added a custom_replace function which like Grunt's replace takes two arguments dest and src and is expected to return a single path.

@andismith

This comment has been minimized.

Show comment Hide comment
@andismith

andismith Feb 27, 2014

Owner

Hi @hellosmithy,

Thanks for your submission. Please could you provide a bit more information about what you have done, and why this is useful... I'm not sure I 100% understand. An example would be great (as would a unit test).

Thanks,
Andi

Owner

andismith commented Feb 27, 2014

Hi @hellosmithy,

Thanks for your submission. Please could you provide a bit more information about what you have done, and why this is useful... I'm not sure I 100% understand. An example would be great (as would a unit test).

Thanks,
Andi

@hellosmithy

This comment has been minimized.

Show comment Hide comment
@hellosmithy

hellosmithy Feb 27, 2014

Hi Andi, I'll add a unit test this weekend. An example use case would be needing to do any kind of more complex regex renames against the dest or src filename.
This is much like the rename used by Grunt when building file objects. See: Building the files object dynamically

I found this particularly useful recently when needing to take information from the src filename via regex and use that to create the dest folder structure for a project. E.g converting from temp/images/product001-front.jpg to images/products/001/thumbnail/front.jpg etc.

Example use:

responsive_images: {
    options: {
        // options
    },
    files: [{
        expand: true,
        src: ['images/**/*.{jpg,gif,png}'],
        cwd: 'temp/',
        custom_dest: 'source/images/{%= name %}/',
        custom_rename: function(dest, src) {
            // custom regex / path changes here
            return dest + src;
        }
    }]
}

Hi Andi, I'll add a unit test this weekend. An example use case would be needing to do any kind of more complex regex renames against the dest or src filename.
This is much like the rename used by Grunt when building file objects. See: Building the files object dynamically

I found this particularly useful recently when needing to take information from the src filename via regex and use that to create the dest folder structure for a project. E.g converting from temp/images/product001-front.jpg to images/products/001/thumbnail/front.jpg etc.

Example use:

responsive_images: {
    options: {
        // options
    },
    files: [{
        expand: true,
        src: ['images/**/*.{jpg,gif,png}'],
        cwd: 'temp/',
        custom_dest: 'source/images/{%= name %}/',
        custom_rename: function(dest, src) {
            // custom regex / path changes here
            return dest + src;
        }
    }]
}
@drwlrsn

This comment has been minimized.

Show comment Hide comment
@drwlrsn

drwlrsn Mar 2, 2014

@hellosmithy I too ran into problems with custom_dest. I would like to preserve the directory structure of my cwd but even with flatten: false the destination is flattened. This means I can't have sub directories named after the image sizes under the original directory name. For example with the cwd: source/ structure that looks like this:

source/
  BigCorp/
    logo.jpg
  MedCorp/
    logo.jpg

It would be nice if I could generate a destination folder structure that looked like:

dest/
  BigCorp/
    small/
      logo.png
    med/
      logo.png
    large/
      logo.png
...

I am not saying I require grunt-responsive-images to do this, but if I could use the the rename method it would make my life easier.

-- Edit --
I just found {%= path =}.

drwlrsn commented Mar 2, 2014

@hellosmithy I too ran into problems with custom_dest. I would like to preserve the directory structure of my cwd but even with flatten: false the destination is flattened. This means I can't have sub directories named after the image sizes under the original directory name. For example with the cwd: source/ structure that looks like this:

source/
  BigCorp/
    logo.jpg
  MedCorp/
    logo.jpg

It would be nice if I could generate a destination folder structure that looked like:

dest/
  BigCorp/
    small/
      logo.png
    med/
      logo.png
    large/
      logo.png
...

I am not saying I require grunt-responsive-images to do this, but if I could use the the rename method it would make my life easier.

-- Edit --
I just found {%= path =}.

@andismith andismith added this to the grunt-responsive-images v0.1.3 milestone Apr 21, 2014

@andismith andismith referenced this pull request Apr 21, 2014

Closed

Question #37

@andismith

This comment has been minimized.

Show comment Hide comment
@andismith

andismith Sep 13, 2016

Owner

Closing as no unit test was added and this is now 2 years old.

Owner

andismith commented Sep 13, 2016

Closing as no unit test was added and this is now 2 years old.

@andismith andismith closed this Sep 13, 2016

@hellosmithy

This comment has been minimized.

Show comment Hide comment
@hellosmithy

hellosmithy Sep 13, 2016

@andismith sorry didn't realise you were waiting on a test file being added. Is this feature now supported? I can't honestly remember what I needed it for now. But happy to write a quick test if needed.

@andismith sorry didn't realise you were waiting on a test file being added. Is this feature now supported? I can't honestly remember what I needed it for now. But happy to write a quick test if needed.

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