Skip to content

Commit e5ce917

Browse files
committed
Add translate="no" attribute to code and pre elements
This commit adds the `translate="no"` attribute to both inline code elements and code blocks to prevent machine translation tools from attempting to translate code snippets. Changes include: 1. Added `translate="no"` attribute to code blocks in block_listing.html.erb 2. Added `translate="no"` attribute to inline code elements in inline_quoted.html.erb 3. Added comprehensive tests to verify the attribute is correctly applied: - Test for inline code elements verifies the attribute is present on <code> tags - Test for code blocks verifies the attribute is present on <pre> tags with data-type="programlisting"
1 parent a0c8279 commit e5ce917

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

htmlbook/block_listing.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<%#encoding:UTF-8%>
2-
<pre<%= @id && %( id="#{@id}") %> data-type="programlisting"<%= attr?('language') ? %( data-code-language="#{attr 'language'}") : nil %><% if (attr 'linenums') == "linenums" %> data-line-numbering="numbered" <% end %><%= attr?('role') ? %( class="#{attr 'role'}") : nil %><%= attr?('data-executable') ? %( data-executable="#{attr 'data-executable'}") : nil %>><%= content %></pre>
2+
<pre<%= @id && %( id="#{@id}") %> translate="no" data-type="programlisting"<%= attr?('language') ? %( data-code-language="#{attr 'language'}") : nil %><% if (attr 'linenums') == "linenums" %> data-line-numbering="numbered" <% end %><%= attr?('role') ? %( class="#{attr 'role'}") : nil %><%= attr?('data-executable') ? %( data-executable="#{attr 'data-executable'}") : nil %>><%= content %></pre>

htmlbook/inline_quoted.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class_attr = (style_class = role) ? %( class="#{style_class}") : nil
33
case @type
44
when :emphasis %><em<%= attr?('role') ? %( #{attr 'role'}) : nil %>><%= @text %></em><%
55
when :strong %><strong<%= attr?('role') ? %( #{attr 'role'}) : nil %>><%= @text %></strong><%
6-
when :monospaced %><code<%= attr?('role') ? %( #{attr 'role'}) : nil %>><%= @text %></code><%
6+
when :monospaced %><code translate="no"<%= attr?('role') ? %( #{attr 'role'}) : nil %>><%= @text %></code><%
77
when :superscript %><sup<%= attr?('role') ? %( #{attr 'role'}) : nil %>><%= @text %></sup><%
88
when :subscript %><sub<%= attr?('role') ? %( #{attr 'role'}) : nil %>><%= @text %></sub><%
99
when :double %><%= class_attr ? %(<span#{class_attr}>&#8220;#{@text}&#8221;</span>) : %(&#8220;#{@text}&#8221;) %><%

spec/htmlbook_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@
277277
html.xpath("//pre[@data-type='programlisting']").text.should == "Hello world"
278278
end
279279

280+
it "should add translate='no' attribute to code blocks" do
281+
html = Nokogiri::HTML(convert("
282+
[source, ruby]
283+
----
284+
def hello
285+
puts 'Hello, world!'
286+
end
287+
----
288+
"))
289+
html.xpath("//pre[@data-type='programlisting']/@translate").text.should == "no"
290+
end
291+
280292
# Tests block_literal template - first markup style
281293
it "should convert literal blocks" do
282294
html = Nokogiri::HTML(convert("
@@ -934,6 +946,11 @@
934946
html.xpath("//p/strong/code").text.should == "userinput"
935947
end
936948

949+
it "should add translate='no' attribute to inline code elements" do
950+
html = Nokogiri::HTML(convert("This para has some ++inline code++ in it."))
951+
html.xpath("//p/code[1]/@translate").text.should == "no"
952+
end
953+
937954
# Tests math in inline_quoted template
938955
it "should convert inline latexmath equations with dollar sign delimeters to use standard delimiters" do
939956
html = Nokogiri::HTML(convert("The Pythagorean Theorem is latexmath:[$a^2 + b^2 = c^2$]"))

0 commit comments

Comments
 (0)