Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:59:11+00:00 2026-06-01T08:59:11+00:00

I’m writing a command-line development utility for my team, using Ruby. I’m trying to

  • 0

I’m writing a command-line development utility for my team, using Ruby.

I’m trying to inspect an HTML document on the filesystem and add new <script> tag before the </head>

Something like:

<html>
  <head>
    <script src="...foo.js"></script>
    <script src="...bar.js"></script>
    <!-- I WANT TO INSERT NEW TEXT HERE -->
  </head>
  <body>
  </body>
</html>

I was thinking of starting with IO.readlines(file_name), comparing each line to a regex, and inserting my new tag ahead of the </head>. Then, merge the whole array back into a new version of the file.

This sounds overly complicated. Who’s got a better way?

For bonus points, it would be great to have the right level of indentation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:59:13+00:00Added an answer on June 1, 2026 at 8:59 am

    In your special case it’s not such a bad idea. If you have that special line in the file, you can easily find it, extract the necessary amount of indentation from the beginning of the line, and replace the whole line with another content. But don’t do it in memory. You can write it to a temporary file while reading the source file, you don’t need to eat up the RAM.

    If the HTML comment was just an example, and that line isn’t there, you still can replace the first occurance of </head> with <script>…</script></head> with a regexp, don’t need to parse HTML. (But this is only true in your special case.) To be frank, you don’t need Ruby either, because a sed command is perfect for this job.

    Or if you mast do other checks, for example that the script is already there or not, then
    use any HTML parser lib/gem. I suggest you hpricot, if you like the concept of jQuery, because hpricot has a very similar approach.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.