Convert HTML to Plain Text in Ruby
Practical Ruby instructions for converting HTML to plain text, checking the output, and fixing common formatting problems.
Before you start
This task runs locally in Ruby. It does not require an online service. Install Ruby if it is not already available, then prepare your HTML as a file or string. For the gem-based method, you also need an internet connection to install the gem before you can use it locally.
Step-by-step instructions
Choose one method. The first method is built into Ruby and uses Nokogiri, which handles real-world HTML more reliably than a simple regular expression. The second method uses a purpose-built plain-text extraction gem.
1) Install the required dependency. In a terminal, run:
gem install nokogiri
2) Load the HTML. You can read it from a file or keep it in a string. For example:
html = File.read("input.html")
3) Parse the HTML and extract the text with Nokogiri:
require "nokogiri"
text = Nokogiri::HTML(html).text
For an XML-style document, use Nokogiri::XML instead of Nokogiri::HTML when the source is strict XML rather than ordinary web HTML.
4) Save the result as plain text:
File.write("output.txt", text)
Alternative: use a text extraction gem
If the extraction behavior is already specified for your project, a gem intended specifically for HTML-to-text conversion can be easier to configure. Install one of the maintained extraction gems, then call its conversion method on the HTML string according to the gem version you installed. Check the gem documentation for the exact method name and options.
Check the output
Open the generated text file and compare it with the original HTML. Tags should be gone, but the text inside tags should remain. In a simple extraction, text from separate inline or block elements may run together without spaces or line breaks. Nokogiri can preserve punctuation and visible wording, but it does not automatically reproduce page layout, headings, tables, or link destinations.
If quality matters, check that important boundaries such as paragraph endings are still understandable. Use a small HTML sample first so you can see exactly how the chosen method changes the output.
Common problems and next steps
One common result is text that lacks spaces between words that were separated by tags. This happens with a basic .text call. Add line breaks around block-level elements before extracting text, or process the document node by node to insert separators. A text extraction gem may handle this with built-in options.
Another issue is unwanted inline CSS or script content. Most extraction paths ignore markup, but script and style tag contents may still become text. Remove them before conversion if the source includes them and they are not wanted in the result. Check the gem version behavior for this edge case.
If non-Latin or accented characters look wrong, verify that the source encoding is declared and read the file with the matching encoding. For local files, opening the file with its actual character encoding is more reliable than guessing from the document.
For camera, audio, or video meeting notes and transcripts, plain text is useful for searching. Keep the original HTML file unchanged until the text output has been checked, so you can rerun the conversion with different separator rules if needed.