History

Merge pull request #2533 from spamguy/tailwind-41

Tailwind CSS v4

Simon Legner committed 1 year ago

Commit 8b3f5522 parents 06ce9ee c05928e3 files · +114 −51Browse files

Unified · Split

3 files
  1. lib/docs/filters/tailwindcss/clean_html.rb +52 −21
  2. lib/docs/filters/tailwindcss/entries.rb +19 −6
  3. lib/docs/scrapers/tailwindcss.rb +43 −24
lib/docs/filters/tailwindcss/clean_html.rb+52−21
@@ -2,26 +2,38 @@ module Docs · 1 unmodified line
2 class Tailwindcss2 class Tailwindcss
3 class CleanHtmlFilter < Filter3 class CleanHtmlFilter < Filter
4 def call4 def call
5 # Move h1 out of wrapper.
6 css('h1').each do |node|
7 doc.prepend_child(node)
8 end
9
5 # Remove main page headers (top level sticky)10 # Remove main page headers (top level sticky)
6 css('#__next > .sticky').remove11 css('#__next > .sticky', 'div.fixed.inset-x-0.top-0').remove
7 # And anything absolutely positioned (fancy floating navigation elements we don't care about)12 # And anything absolutely positioned (fancy floating navigation elements we don't care about)
8 css('#__next .absolute').remove13 css('#__next .absolute', '.fixed').remove
9 # Remove the left-navigation we scraped14 # Remove the left-navigation we scraped
10 css('nav').remove15 css('nav').remove
11 16
12 css('svg').remove if root_page?17 css('svg').remove if root_page?
13 18
14 # Remove the duplicate category name at the top of the page - redundant
15 at_css('header#header > div:first-child > p:first-child').remove
16
17 # Remove the right navigation sidebar19 # Remove the right navigation sidebar
18 at_css('header#header').parent.css('> div:has(h5:contains("On this page"))').remove20 css('header#header', 'p[data-section]').each do |node|
21 node.parent.css('> div:has(h5:contains("On this page"))').remove # v3
22
23 node.parent.parent.css('div.max-xl\\:hidden').remove # v4
24 end
25
26 # Remove the duplicate category name at the top of the page - redundant
27 css(
28 'header#header > div:first-child > p:first-child', # v3
29 'p[data-section]' # v4
30 ).remove
19 31
20 # Remove footer + prev/next navigation32 # Remove footer + prev/next navigation
21 at_css('footer').remove33 css('footer', '.row-start-5').remove
22 34
23 # Handle long lists of class reference that otherwise span several scrolled pages35 # Handle long lists of class reference that otherwise span several scrolled pages
24 if class_reference = at_css('#class-reference')36 if class_reference = at_css('#class-reference', '#quick-reference')
25 reference_container = class_reference.parent37 reference_container = class_reference.parent
26 classes_container = reference_container.children.reject{ |child| child == class_reference }.first38 classes_container = reference_container.children.reject{ |child| child == class_reference }.first
27 39
@@ -33,7 +45,7 @@ module Docs · 5 unmodified lines
33 end45 end
34 46
35 # Remove border color preview column as it isn't displayed anyway47 # Remove border color preview column as it isn't displayed anyway
36 if result[:path] == "border-color" and class_reference = at_css('#class-reference')48 if result[:path] == "border-color" and class_reference = at_css('#class-reference', '#quick-reference')
37 class_reference.parent.css("thead th:nth-child(3)").remove49 class_reference.parent.css("thead th:nth-child(3)").remove
38 class_reference.parent.css("tbody td:nth-child(3)").remove50 class_reference.parent.css("tbody td:nth-child(3)").remove
39 end51 end
@@ -59,29 +71,48 @@ module Docs · 19 unmodified lines
59 end71 end
60 72
61 # Remove buttons to expand lists - those are already expanded and the button is useless73 # Remove buttons to expand lists - those are already expanded and the button is useless
62 css('div > button:contains("Show all classes")').each do |node|74 css(
75 'div > button:contains("Show all classes")',
76 'div > button:contains("Show more")'
77 ).each do |node|
63 node.parent.remove78 node.parent.remove
64 end79 end
65 80
66 # Remove class examples - not part of devdocs styleguide? (similar to bootstrap)81 # Remove class examples - not part of devdocs styleguide? (similar to bootstrap)
67 # Refer to https://github.com/freeCodeCamp/devdocs/pull/1534#pullrequestreview-64981893682 # Refer to https://github.com/freeCodeCamp/devdocs/pull/1534#pullrequestreview-649818936
68 css('.not-prose').each do |node|83 css('.mt-4.-mb-3', 'figure', 'svg', '.flex.space-x-2').remove
69 if node.parent.children.length == 1
70 node.parent.remove
71 else
72 node.remove
73 end
74 end
75 84
76 # Properly format code examples85 # Properly format code examples.
77 css('pre > code:first-child').each do |node|86 css('pre > code:first-child').each do |node|
78 node.parent['data-language'] = node['class'][/language-(\w+)/, 1] if node['class'] and node['class'][/language-(\w+)/]87 # v4 doesn't provide language context, so it must be inferred imperfectly.
79 node.parent.content = node.parent.content88 node.parent['data-language'] =
89 if node.content.include?('function')
90 'jsx'
91 elsif node.content.include?("</")
92 'html'
93 else
94 'css'
95 end
96
97 node.parent.content =
98 if version == '3'
99 node.parent.content
100 else
101 node.css('.line').map(&:content).join("\n")
102 end
103 end
104
105 # Remove headers some code examples have.
106 css('.flex.text-slate-400.text-xs.leading-6', '.px-3.pt-0\\.5.pb-1\\.5').remove
107
108 # Strip anchor from headers
109 css('h2', 'h3').each do |node|
110 node.content = node.inner_text
80 end111 end
81 112
82 @doc.traverse { |node| cleanup_tailwind_classes(node) }113 @doc.traverse { |node| cleanup_tailwind_classes(node) }
83 114
84 #remove weird <hr> (https://github.com/damms005/devdocs/commit/8c9fbd859b71a2525b94a35ea994393ce2b6fedb#commitcomment-50091018)115 # Remove weird <hr> (https://github.com/damms005/devdocs/commit/8c9fbd859b71a2525b94a35ea994393ce2b6fedb#commitcomment-50091018)
85 css('hr').remove116 css('hr').remove
86 117
87 doc118 doc
lib/docs/filters/tailwindcss/entries.rb+19−6
@@ -3,22 +3,35 @@ module Docs · 2 unmodified lines
3 class EntriesFilter < Docs::EntriesFilter3 class EntriesFilter < Docs::EntriesFilter
4 def get_type4 def get_type
5 # We are only interested in children list items5 # We are only interested in children list items
6 selector = "nav li li a[href='#{result[:path]}']"6
7 7
8 anchor = at_css(selector)8 anchor = at_css(get_selector)
9 category_list = anchor.ancestors('li')[1]9 title =
10 title = category_list.css('h5')10 if version == '3'
11 anchor.ancestors('li')[1].css('h5')
12 else
13 anchor.ancestors('ul').last.previous_element
14 end
11 15
12 return title.inner_text16 return title.inner_text
13 end17 end
14 18
15 def get_name19 def get_name
16 # We are only interested in children list items20 # We are only interested in children list items
17 selector = "nav li li a[href='#{result[:path]}']"21 item = at_css(get_selector)
18 item = at_css(selector)
19 22
20 return item.inner_text23 return item.inner_text
21 end24 end
25
26 private
27
28 def get_selector
29 if version == '3'
30 "nav li li a[href='#{result[:path]}']"
31 else
32 "nav li a[href*='#{result[:path]}']"
33 end
34 end
22 end35 end
23 end36 end
24end37end
lib/docs/scrapers/tailwindcss.rb+43−24
@@ -3,9 +3,7 @@ module Docs · 2 unmodified lines
3 self.name = 'Tailwind CSS'3 self.name = 'Tailwind CSS'
4 self.type = 'tailwindcss'4 self.type = 'tailwindcss'
5 self.slug = 'tailwindcss'5 self.slug = 'tailwindcss'
6 self.base_url = 'https://tailwindcss.com/docs'
7 self.root_path = '/'6 self.root_path = '/'
8 self.release = '3.3.2'
9 self.links = {7 self.links = {
10 home: 'https://tailwindcss.com/',8 home: 'https://tailwindcss.com/',
11 code: 'https://github.com/tailwindlabs/tailwindcss'9 code: 'https://github.com/tailwindlabs/tailwindcss'
@@ -16,27 +14,6 @@ module Docs · 4 unmodified lines
16 # Disable the clean text filter which removes empty nodes - we'll do it ourselves more selectively14 # Disable the clean text filter which removes empty nodes - we'll do it ourselves more selectively
17 text_filters.replace("clean_text", "tailwindcss/noop")15 text_filters.replace("clean_text", "tailwindcss/noop")
18 16
19 # Fix redirects from older tailwind 2 docs
20 options[:fix_urls] = lambda do |url|
21 if url.include? "installation/"
22 break "/docs/installation"
23 end
24
25 if url.end_with? "/breakpoints"
26 break "/docs/screens#{/#.*$/.match(url)}"
27 end
28 if url.end_with? "/adding-base-styles"
29 break "/docs/adding-custom-styles#adding-base-styles"
30 end
31 if url.end_with? "/ring-opacity"
32 break "/docs/ring-color#changing-the-opacity"
33 end
34
35 if url.match(/\/colors#?/)
36 break "/docs/customizing-colors#{/#.*$/.match(url)}"
37 end
38 end
39
40 options[:skip_patterns] = [17 options[:skip_patterns] = [
41 # Skip setup instructions18 # Skip setup instructions
42 /\/browser-support$/,19 /\/browser-support$/,
@@ -49,9 +26,51 @@ module Docs · 6 unmodified lines
49 26
50 #Obtainable from https://github.com/tailwindlabs/tailwindcss/blob/master/LICENSE27 #Obtainable from https://github.com/tailwindlabs/tailwindcss/blob/master/LICENSE
51 options[:attribution] = <<-HTML28 options[:attribution] = <<-HTML
52 &copy; 2022 Tailwind Labs Inc.29 &copy; Tailwind Labs Inc.
53 HTML30 HTML
54 31
32 version do
33 self.release = '4.1.11'
34 self.base_url = 'https://tailwindcss.com/docs'
35
36 # Fix redirects
37 options[:fix_urls] = lambda do |url|
38 if url.include? "installation/"
39 break "/docs/installation"
40 end
41
42 if url.end_with? "text-color"
43 break "/docs/color"
44 end
45 end
46 end
47
48 version '3' do
49 self.release = '3.4.17'
50 self.base_url = 'https://v3.tailwindcss.com/docs'
51
52 # Fix redirects from older tailwind 2 docs
53 options[:fix_urls] = lambda do |url|
54 if url.include? "installation/"
55 break "/docs/installation"
56 end
57
58 if url.end_with? "/breakpoints"
59 break "/docs/screens#{/#.*$/.match(url)}"
60 end
61 if url.end_with? "/adding-base-styles"
62 break "/docs/adding-custom-styles#adding-base-styles"
63 end
64 if url.end_with? "/ring-opacity"
65 break "/docs/ring-color#changing-the-opacity"
66 end
67
68 if url.match(/\/colors#?/)
69 break "/docs/customizing-colors#{/#.*$/.match(url)}"
70 end
71 end
72 end
73
55 def get_latest_version(opts)74 def get_latest_version(opts)
56 get_latest_github_release('tailwindlabs', 'tailwindcss', opts)75 get_latest_github_release('tailwindlabs', 'tailwindcss', opts)
57 end76 end