What's the most efficient way to read, rebuild, and replace a block of content in a file using Gulp?
Asked Answered
A

1

3

I'm creating a system that can read through any file (php, jsp, html, etc), locate block tags, and do a replacement based on the information in the block tag.

Code I would write into my file:

<!-- build:<name> -->
    {
        "testObject": {
             "name": "jonathan",
             "number": 3,
             "male": true
         }
    }
<!-- endBuild -->

Desired replacement:

<h1>Jonathan</h1>
<p>is a male and is positioned at #3.</p>

As you may notice, I've used components of gulp-html-replace. I've researched gulp-data and know how to use gulp.src and gulp.dest to build out files. Just missing the read, build, and replace the object step. Ideally this will work on multiple object instances throughout the document. Thanks.

Alicia answered 31/3, 2016 at 13:32 Comment(0)
T
3

Because Gulp uses a pipe system and all the files become streams, you can write your own pipe/plug-in to process files in very specific ways. Check out Gulp Writing Plugins, section Modifying file content.

Notable Mention

gulp-replace might actually do the trick

Takeo answered 31/3, 2016 at 14:11 Comment(4)
I think you're on to something here. gulp-replace allows a regex search parameter and stores it for use. I'm going to try it out with just that plugin before trying to build up one. Thanks!Alicia
gulp-replace also uses a string as input in addition to regex.Takeo
Yeah, I saw that too. Since my objects will change throughout the page, I think regex is the way to go for this use case.Alicia
Awesomeness. If you can, mark this as the right answer. Cheers.Takeo

© 2022 - 2024 — McMap. All rights reserved.