html - 如何使用带有 srcset 属性的图片元素从 Asciidoc 源输出具有响应式图像的 HTML5?
问题描述
所以,如果我想要看起来像这样的 HTML5 输出:
<picture>
<source type="image/svg+xml" srcset="pyramid.svg">
<source type="image/webp" srcset="pyramid.webp">
<img src="pyramid.png" alt="regular pyramid built from four equilateral triangles">
</picture>
(取自这里)
我的 AsciiDoc 源代码会是什么样子?也许是一个带有多个图像的图形,它使用自定义后端转换为这个图形,还是什么?这甚至可以使用 AsciiDoc/Asciidoctor 来实现吗?
解决方案
这绝对是可能的!
使用内置image
宏,您可以定义如下内容:
image::pyramid.png[regular pyramid built from four equilateral triangles,sources="pyramid.svg,pyramid.webp"]
请注意,该sources
属性将在Image
块上可用。
然后,使用自定义转换器,您可以检索此属性的值以构建<source>
元素(包装在<picture>
元素中)。
这是一个简化的示例:
MIME_TYPES = {
".svg" => 'image/svg+xml',
".webp" => 'image/webp'
}
def convert_image node
target = node.attr 'target'
sources = (node.attr 'sources', '').split(',').map do |src|
%(<source type="#{MIME_TYPES[File.extname src]}" srcset="#{src}">)
end
%(<picture>
#{sources.join("\n")}
<img src="#{target}" alt="#{node.alt}">
</picture>)
end
您当然可以使用附加属性来做更高级的事情。
请记住,也可以使用扩展来扩展 AsciiDoc 语法。话虽如此,在这种情况下,我认为最好使用内置image
宏来保留语义信息。
推荐阅读
- racket - 定义:函数体只需要一个表达式,但发现了 1 个额外的部分
- android - SQLiteLog:(1)“事务”附近:语法错误
- flutter - flutter_driver 测试的资产加载
- python - 从 ML 管道调用脚本时无法导入“BlockBlobService”
- php - 如何获取使条件为 TRUE 的 if 条件的值
- python - 有没有办法在 Termux for Android 中使用 webbrowser 模块?
- c++ - 头文件中的 c++ 17 内联变量在不被引用时会被优化吗
- c - Windows上的C线程,如何传递参数?
- python - 有没有办法安全地使用套接字库
- maven - Entitymanager 查询总是在 JEE webapp 上使用 JPA 返回一个空列表