Great follow-up! If all the lines in your .xhtml files are already wrapped in a class like .indent, you don’t need to wrap them again. Instead, you can simply target every other .indent element using CSS.
✅ Here's the simplified approach tailored to your case:
🧭 Goal
Make every .indent element alternate between:
- Left-to-right (LTR) on odd lines
- Right-to-left (RTL) on even lines
This will simulate boustrophedon layout.
👨💻 1. CSS: Modify or add to your stylesheet
Find your EPUB’s stylesheet (stylesheet.css, style.css, etc.). Open it and add the following CSS:
.indent:nth-of-type(odd) {
direction: ltr;
unicode-bidi: embed;
}
.indent:nth-of-type(even) {
direction: rtl;
unicode-bidi: embed;
}
🔍 A Note on unicode-bidi: embed
This CSS property ensures that the direction is strictly applied to each element. Without it, some EPUB readers may not apply the direction truly (especially for Latin text).
🧩 Example XHTML
Assuming your XHTML already looks like this:
<p class="indent">The forest was dark and deep.</p>
<p class="indent">And yet, the wind whispered gently.</p>
<p class="indent">Nothing stirred in the underbrush.</p>
With the CSS above:
- The 1st paragraph is LTR.
- The 2nd paragraph is RTL.
- The 3rd paragraph is LTR.
💡 The effect alternates as desired without extra HTML changes.
⚠️ Important: Type vs Child
Make sure your .indent elements are the same tag type and inside the same parent tag, or :nth-of-type may not work reliably.
✅ Good:
<body>
<p class="indent">Line 1</p>
<p class="indent">Line 2</p>
</body>
⚠️ Problematic:
If your lines are:
<div class="indent">Line 1</div>
<p class="indent">Line 2</p>
<span class="indent">Line 3</span>
:nth-of-type won’t apply as expected because the tags differ.
In that case, switch to:
.indent:nth-child(odd) { ... }
.indent:nth-child(even) { ... }
BUT :nth-child counts all elements, not just .indent. So the layout might jump if .indent elements are interrupted by other tags (like images, breaks, headings, etc.).
👉 If your layout is uniform (e.g., only .indent lines inside <body> or <div class="text">), you're good.
🧪 2. Validate in an EPUB reader
Repackage your EPUB using ZIP and rename it back to .epub. Then test in:
- Calibre
- Apple Books (macOS)
- Google Play Books (web, Android)
- Adobe Digital Editions
📌 Note: Some EPUB readers may partially implement CSS, so test in more than one if you experience issues.
📎 Optional Bonus: Add rtl marker for punctuation rendering issues
Some browsers/readers mess up RTL lines, especially punctuation. You can add a "Right-to-Left Mark" (RLM = \u200F) manually or via script for better rendering:
<p class="indent">This line starts RTL.</p>
Or in Python:
line = '\u200F' + line # prepend RLM character
✅ Summary
Since your lines are all in .indent:
- You don’t need to rewrap them
- Just apply
direction: rtl / ltr via CSS + unicode-bidi
- Choose
:nth-of-type (preferred) or :nth-child based on HTML structure
- Test for quirks in various readers
Would you like help inspecting your XHTML layout or writing a validator script to see if :nth-of-type will work reliably? Let me know!