Skip to content

Instantly share code, notes, and snippets.

@rubys
Created November 7, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubys/bdd93e7355fb906a7268 to your computer and use it in GitHub Desktop.
Save rubys/bdd93e7355fb906a7268 to your computer and use it in GitHub Desktop.
anolis2bikeshed.rb for URL Standard
src = Dir.chdir File.dirname(__FILE__) do
system 'git checkout url.src.html'
File.read('url.src.html')
end
# replace boilerplate with metadata
src.sub! /\A.*?\n<h2/m, <<-EOF + "\n<h2"
<pre class="metadata">
Title: URL Standard
Group: WHATWG
H1: URL
Shortname: url
Status: LS
No Editor: true
Abstract: This specification provides APIs for creating, parsing, and working with universal identifiers.
Logo: https://resources.whatwg.org/logo-url.svg
!Version History: <a href="https://github.com/whatwg/url/commits">https://github.com/whatwg/url/commits</a> <a href="https://twitter.com/urlstandard">@urlstandard</a>
!Participate: <a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?product=WHATWG&amp;component=URL">file a bug</a> (<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&amp;component=URL&amp;resolution=---">open bugs</a>)
!Participate: <a href="https://whatwg.org/mailing-list">whatwg@whatwg.org</a> (<a href="https://whatwg.org/mailing-list#specs">archives</a>)
!Participate: <a href="http://wiki.whatwg.org/wiki/IRC">IRC: #whatwg on Freenode</a>
</pre>
EOF
# remove TOC
src.sub! /^<h2[^\n]*>Table of Contents<\/h2>.*?<h2/m, '<h2'
# remove Conformance
src.sub! /^<h2[^\n]*>Conformance<\/h2>.*?<h2/m, '<h2'
# remove References
src.sub! /^<h2[^\n]*>References<\/h2>.*?<h2/m, '<h2'
# add ids to headers
src.gsub! /<h\d.*?>(.*?)<\/h\d>/ do |head|
id = $1.gsub(/<.*?>/, '').gsub(' ', '-').downcase
id = '"' + id + '"' unless id =~ /^[-\w]+$/
head[/<h\d()/, 1] = " id=#{id}" unless head =~ /<h\d id=/
head
end
# change document references to bikeshed format
externs = {
"URI" => "RFC3986",
"IRI" => "RFC3987",
"ORIGIN" => "RFC6454",
"IPV6" => "RFC4291",
"IPV6TEXT" => "RFC5952"
}
src.gsub! /<span data-anolis-ref.*?>(.*?)<\/span>/ do |ref|
name = externs[$1] || $1
"[[#{'!' unless ref.include? 'informative'}#{name}]]"
end
# change links from <span> to <a>
src.gsub! /<span(.*?>.*?)<\/span>/ do |link|
if link.include? 'XXX'
link
else
"<a#{$1}</a>"
end
end
# zap var title
src.gsub! /<var title>/, '<var>'
# interpret <code> links
src.gsub! /<code(.*?)>(.*?)<\/code>/ do |link|
term = $2
if link.include? '<code title>'
link.sub '<code title>', '<code>'
elsif link.include? 'title=dom-URL-'
if $2 == 'href'
"<a stringifier for=URLUtils><code>#{$2}</code></a>"
else
"<a attribute for=URLUtils><code>#{$2}</code></a>"
end
elsif
%w(
url
urlsearchparams
urlutils
urlutilsreadonly
).include? $2.downcase
then
"<a interface><code#{$1}>#{$2}</code></a>"
elsif %w(file:/path/ / messageevent).include? $2.downcase
"<code#{$1}>#{$2}</code>"
else
"<a#{$1}><code>#{$2}</code></a>"
end
end
# eliminate some self-referential links
src.gsub! /(<dfn.*?>)<a>(.*?)<\/a>(<\/dfn>)/, '\1\2\3'
# change data-anolis-spec attributes to hrefs
externs = {
# "encoding" => "https://encoding.spec.whatwg.org/#",
# "idna" => "http://www.unicode.org/reports/tr46/#",
# "fileapi" => "http://dev.w3.org/2006/webapi/FileAPI/#",
# "html" => "https://html.spec.whatwg.org/multipage/#",
# "dom" => "https://dom.spec.whatwg.org/#",
# "webidl" => "http://heycam.github.io/webidl/#dfn-"
}
src.gsub! /<a data-anolis-spec=(\w+)(.*?)>(.*?)<\/a>/ do |link|
spec, remainder, text = $1, $2, $3
title = link[/title="(.*?)"/,1] || link[/title=([-\w]+)/,1] || text
if externs.include? spec
"<a href=#{externs[spec]}#{title.gsub(' ', '-')}#{remainder}>#{text}</a>"
else
"<a#{remainder}>#{text}</a>"
end
end
# DISABLED
# change unadorned <i> elements to use <var> instead
src.gsub /<i>(.*?)<\/i>/ do
"<var>#{$1}</var>"
end
# add parens to function <dfn>s
src.gsub! /<dfn title=(.*?)>(<a>)?<code>\w+\(.*?\<\/dfn>/ do |fn|
fn.sub $1, '"' + $1 + '()"'
end
# fix for html5lib "DataLossWarning: Coercing non-XML name"
src.sub! ">><", ">&gt;<"
# fix application/x-www-form-urlencoded id collision TODO
src.gsub! /<(dfn|code).*>application\/x-www-form-urlencoded<\S*\/\1>/ do |axwfu|
if axwfu =~ /<dfn/
axwfu[/<dfn()/, 1] = ' id=application-x-www-form-urlencoded-0'
elsif axwfu !~ /code title/
axwfu[/<code()/, 1] = ' title=application-x-www-form-urlencoded-0'
end
axwfu
end
# idl
src.gsub! /<pre class=idl>.*?<\/pre?>/m do |idl|
"<pre class=idl>#{idl.gsub(/<.*?>/, '')}</pre>"
end
%w(URL URLSearchParams).each do |constructor|
src.gsub! "<dfn title=\"dom-#{constructor}()\">",
"<dfn constructor dfn-for=#{constructor}>"
end
%w(domainToASCII domainToUnicode).each do |method|
src.gsub! "dfn title=\"dom-URL-#{method}()\"", 'dfn method dfn-for=URL'
end
URLUTILS_ATTRS = %w(
href origin protocol username password hostname host port pathname
searchParams search hash
)
URLUTILSREADONLY_ATTRS = %w(
href origin protocol hostname host port pathname search hash
)
# TODO: Constructor, Stringifier
URLUTILS_ATTRS.each do |attr|
type = (attr=='href' ? 'stringifier' : 'attribute')
if URLUTILSREADONLY_ATTRS.include? attr
src.gsub!(/(<dfn title=dom-URL-#{attr}.*?<\/dfn>)/,
'<dfn ' + type + ' dfn-for=URLUtilsReadOnly>\1</dfn>')
end
src.gsub!("dfn title=dom-URL-#{attr}", "dfn #{type} dfn-for=URLUtils")
src.gsub!("dom-URL-#{attr}", "dom-URLUtils-#{attr}")
end
src.gsub! /<dfn title="dom-URLSearchParams-\w+\(\)">/,
"<dfn method dfn-for=URLSearchParams>"
src.gsub! '<dfn title=dom-URLSearchParams-stringifier>',
'<dfn stringifier dfn-for=URLSearchParams>'
# swap a and code
src.gsub! /(<a.*?>)(<code.*?>)(.*?)(<\/code>)(<\/a>)/, '\2\1\3\5\4'
# DISABLED
# comment out problematic line for now
# src[/().* implements .*/, 1] = '// '
src += "\n" + <<-EOF
<pre class="biblio"
{
"IDNA": {
"authors": [
"Mark Davis",
"Michel Suignard"
],
"href": "http://www.unicode.org/reports/tr46/",
"title": "Unicode IDNA Compatibility Processing",
"publisher": "Unicode Consortium"
},
"DOM": {
"authors": [
"Anne van Kesteren",
"Aryeh Gregor",
"Ms2ger"
],
"href": "https://dom.spec.whatwg.org/",
"title": "DOM",
"publisher": "WHATWG"
},
"ENCODING": {
"authors": [
"Anne van Kesteren"
],
"href": "https://encoding.spec.whatwg.org/",
"title": "Encoding",
"publisher": "WHATWG"
}
}
</pre>
<pre class="anchors">
[
{
"linkingText": "ascii whitespace",
"url": "https://encoding.spec.whatwg.org/#ascii-whitespace",
"shortname": "encoding", "type": "dfn", "level": 2, "status": "current"
},
{
"linkingText": "blob",
"url": "http://dev.w3.org/2006/webapi/FileAPI/#blob",
"shortname": "fileapi", "type": "dfn", "level": 2, "status": "current"
},
{
"linkingText": "blob url store",
"url": "http://dev.w3.org/2006/webapi/FileAPI/#BlobURLStore",
"shortname": "fileapi", "type": "dfn", "level": 2, "status": "current"
},
{
"linkingText": "concept-attribute-value",
"url": "https://dom.spec.whatwg.org/#concept-attribute-value",
"shortname": "webidl", "type": "dfn", "level": 2, "status": "current"
},
{
"linkingText": "concept-document",
"url": "https://dom.spec.whatwg.org/#concept-document",
"shortname": "dom", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "concept-encoding-get",
"url": "https://encoding.spec.whatwg.org/#concept-encoding-get",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "context object",
"url": "https://dom.spec.whatwg.org/#context-object",
"shortname": "dom", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "decoder",
"url": "https://encoding.spec.whatwg.org/#decoder",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "dom-node-baseuri",
"url": "https://dom.spec.whatwg.org/#dom-node-baseuri",
"shortname": "dom", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "encoding",
"url": "https://encoding.spec.whatwg.org/#encoding",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "error",
"url": "https://encoding.spec.whatwg.org/#error",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "eventsource",
"url": "https://html.spec.whatwg.org/multipage/comms.html#eventsource",
"shortname": "html", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "hashchangeevent",
"url": "https://html.spec.whatwg.org/multipage/comms.html#hashchangeevent",
"shortname": "html", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "structured clone",
"url": "https://html.spec.whatwg.org/multipage/infrastructure.html#structured-clone",
"shortname": "html", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "throw",
"url": "http://heycam.github.io/webidl/#dfn-throw",
"shortname": "webidl", "type": "dfn", "level": 2, "status": "current"
},
{
"linkingText": "toascii",
"url": "http://www.unicode.org/reports/tr46/#ToASCII",
"shortname": "idna", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "tounicode",
"url": "http://www.unicode.org/reports/tr46/#ToUnicode",
"shortname": "idna", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "utf-8",
"url": "https://encoding.spec.whatwg.org/#utf-8",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "utf-8 decode without bom",
"url": "https://encoding.spec.whatwg.org/#utf-8-decode-without-bom",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "utf-8 encode",
"url": "https://encoding.spec.whatwg.org/#utf-8-encode",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "utf-8 decoder",
"url": "https://encoding.spec.whatwg.org/#utf-8-decoder",
"shortname": "encoding", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "unicode serialization of an origin",
"url": "https://html.spec.whatwg.org/multipage/browsers.html#unicode-serialisation-of-an-origin",
"shortname": "html", "type": "dfn", "level": 1, "status": "current"
},
{
"linkingText": "value pairs to iterate over",
"url": "http://heycam.github.io/webidl/#dfn-value-pairs-to-iterate-over",
"shortname": "webidl", "type": "dfn", "level": 2, "status": "current"
}
]
</pre>
EOF
Dir.chdir File.expand_path('../../url-bikeshed', __FILE__) do
File.open('url.bs', 'w') {|file| file.write src}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment