Purpose
This tutorial teaches you how to convert HTML documents to AsciiDoc using Ceradoc’s HTML input converter.
Prerequisites
-
Completed Basic Parsing Tutorial
-
Basic understanding of HTML structure
-
Familiarity with AsciiDoc syntax
Tutorial Overview
You’ll learn to:
-
Convert HTML to AsciiDoc
-
Handle common HTML elements
-
Customize the conversion process
-
Handle complex HTML structures
HTML Input Converter
Ceradoc includes an HTML input converter that transforms HTML into the Document Model, which can then be serialized to AsciiDoc.
require 'ceradoc'
html = "<h1>Title</h1><p>Content here.</p>"
# Parse HTML to Document Model
doc = Ceradoc::Input::HTML.convert(html)
# Serialize to AsciiDoc
asciidoc = doc.to_adocExample 1: Basic HTML Conversion
The Code
require 'ceradoc'
html = <<~HTML
<!DOCTYPE html>
<html>
<head><title>My Document</title></head>
<body>
<h1>Main Title</h1>
<p>This is a paragraph.</p>
</body>
</html>
HTML
# Convert HTML to Document Model
doc = Ceradoc::Input::HTML.convert(html)
# Serialize to AsciiDoc
puts "Converted AsciiDoc:"
puts doc.to_adocExample 2: Converting Headings
Example 3: Converting Lists
Example 4: Converting Links and Formatting
Example 5: Converting Tables
Example 6: Converting Code Blocks
HTML Conversion Reference
Supported Elements
| HTML Element | AsciiDoc Equivalent | |-------------|-------------------| | <h1> - <h6> | = heading levels | | <p> | Paragraph | | <ul>, <li> | Unordered list () | | <ol>, <li> | Ordered list (.) | | <strong> | Bold (*text) | | <em> | Italic (text) | | <code> | Monospace ( ) | | text <a> | Link | | <table> | Table | | <pre><code> | Listing block |
Customizing Conversion
Complete Example: Web Page Conversion
The Code
require 'ceradoc'
html = <<~HTML
<!DOCTYPE html>
<html>
<head>
<title>My Article</title>
</head>
<body>
<article>
<h1>Understanding Round-tripping</h1>
<p>Round-tripping is the process of <strong>parsing</strong> a document,
<em>serializing</em> it, and <code>parsing</code> it again.</p>
<h2>Key Concepts</h2>
<ul>
<li>Semantic preservation</li>
<li>Structure equivalence</li>
<li>Content fidelity</li>
</ul>
<h2>Example</h2>
<pre><code># Parse and serialize
doc = Ceradoc.parse(input)
output = doc.to_adoc</code></pre>
<p>See <a href="https://example.com">the documentation</a> for more.</p>
</article>
</body>
</html>
HTML
doc = Ceradoc::Input::HTML.convert(html)
puts "Converted AsciiDoc:"
puts doc.to_adocExpected Output
Converted AsciiDoc:
= Understanding Round-tripping
Round-tripping is the process of *parsing* a document,
_serializing_ it, and `parsing` it again.
== Key Concepts
* Semantic preservation
* Structure equivalence
* Content fidelity
== Example
Parse and serialize code:
# Parse and serialize
doc = Ceradoc.parse(input)
output = doc.to_adoc
See https://example.com[the documentation] for more.Summary
In this tutorial you learned:
-
How to convert HTML to AsciiDoc
-
Which HTML elements are supported
-
How to customize the conversion process
-
How to handle complex HTML structures
Exercises
Practice your skills:
-
Convert a real HTML document from the web
-
Write a custom converter for a specific HTML element
-
Handle HTML with class/id attributes
-
Create a script that batch-converts HTML files
Next Steps
-
Custom Blocks Guide - Extend Ceradoc
-
Output Formats - More format options
-
Architecture - Understanding input processing