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.