date_fns.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. module Docs
  2. class DateFns < FileScraper
  3. self.name = 'date-fns'
  4. self.slug = 'date_fns'
  5. self.type = 'simple'
  6. self.links = {
  7. home: 'https://date-fns.org/',
  8. code: 'https://github.com/date-fns/date-fns'
  9. }
  10. self.release = '2.29.2'
  11. self.base_url = "https://date-fns.org/v#{self.release}/docs/"
  12. # https://github.com/date-fns/date-fns/blob/main/LICENSE.md
  13. options[:attribution] = <<-HTML
  14. &copy; 2021 Sasha Koss and Lesha Koss<br>
  15. Licensed under the MIT License.
  16. HTML
  17. def build_pages
  18. markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, fenced_code_blocks: true, tables: true)
  19. md_files = %w(esm.md fp.md gettingStarted.md i18n.md i18nContributionGuide.md release.md timeZones.md unicodeTokens.md upgradeGuide.md webpack.md)
  20. md_files.each do |md_file|
  21. md_string = request_one("docs/#{md_file}").body
  22. md_file = 'index.md' if md_file == 'gettingStarted.md'
  23. name = md_string.match(/^#([^\n]+)/)[1]
  24. path = md_file.sub '.md', ''
  25. page = {
  26. path: path,
  27. store_path: "#{path}.html",
  28. output: markdown.render(md_string),
  29. entries: [Entry.new(name, path, 'General')]
  30. }
  31. yield page
  32. end
  33. docs = JSON.parse(request_one('tmp/docs.json').body)
  34. docs.each do |type, features|
  35. features.each do |feature|
  36. name = feature['title']
  37. feature_id = feature['urlId']
  38. next if feature_id.start_with?('fp/')
  39. next if feature['type'] != 'jsdoc'
  40. # fix description table on https://date-fns.org/v2.29.2/docs/parse
  41. feature['content']['description'].sub! "\n| Unit", "\n\n| Unit"
  42. feature['content']['description'].sub! "\nNotes:\n", "\n\nNotes:\n"
  43. page = {
  44. path: name,
  45. store_path: "#{feature_id}.html",
  46. output: ERB.new(PAGE_ERB).result(binding),
  47. entries: [Entry.new(name, feature_id, type)]
  48. }
  49. yield page
  50. end
  51. end
  52. end
  53. PAGE_ERB = <<-HTML.strip_heredoc
  54. <h1><%= feature['title'] %></h1>
  55. <p><%= feature['description'] %></p>
  56. <h2>Description</h2>
  57. <p><%= markdown.render feature['content']['description'] %></p>
  58. <% if feature['usage'] %>
  59. <h2>Usage</h2>
  60. <% feature['usage'].each do |_, usage| %>
  61. <pre data-language="javascript"><%= '// ' + usage['title'] + '\n' %><%= usage['code'] %></pre>
  62. <% end %>
  63. <% end %>
  64. <% if feature['syntax'] %>
  65. <h2>Syntax</h2>
  66. <pre data-language="javascript"><%= feature['syntax'] %></pre>
  67. <% end %>
  68. <% if feature['content']['properties'] %>
  69. <h2>Properties</h2>
  70. <table>
  71. <tr>
  72. <th>Name</th>
  73. <th>Type</th>
  74. <th>Description</th>
  75. </tr>
  76. <% feature['content']['properties'].each do |param| %>
  77. <tr>
  78. <td><code><%= param['name'] %></code></td>
  79. <td><code><%= param['type']['names'].join ' ' %></code></td>
  80. <td><%= markdown.render param['description'] || '' %></td>
  81. </tr>
  82. <% end %>
  83. </table>
  84. <% end %>
  85. <% if feature['content']['params'] %>
  86. <h2>Arguments</h2>
  87. <table>
  88. <tr>
  89. <th>Name</th>
  90. <th>Description</th>
  91. </tr>
  92. <% feature['content']['params'].each do |param| %>
  93. <tr>
  94. <td><code><%= param['name'] %></code></td>
  95. <td><%= markdown.render param['description'] || '' %></td>
  96. </tr>
  97. <% end %>
  98. </table>
  99. <% end %>
  100. <% if feature['content']['returns'] %>
  101. <h2>Returns</h2>
  102. <table>
  103. <tr>
  104. <th>Description</th>
  105. </tr>
  106. <% feature['content']['returns'].each do |param| %>
  107. <tr>
  108. <td><%= markdown.render param['description'] || '' %></td>
  109. </tr>
  110. <% end %>
  111. </table>
  112. <% end %>
  113. <% if feature['content']['exceptions'] %>
  114. <h2>Exceptions</h2>
  115. <table>
  116. <tr>
  117. <th>Type</th>
  118. <th>Description</th>
  119. </tr>
  120. <% feature['content']['exceptions'].each do |param| %>
  121. <tr>
  122. <td><code><%= param['type']['names'].join ' ' %></code></td>
  123. <td><%= markdown.render param['description'] || '' %></td>
  124. </tr>
  125. <% end %>
  126. </table>
  127. <% end %>
  128. <% if feature['content']['examples'] %>
  129. <h2>Examples</h2>
  130. <% feature['content']['examples'].each do |example| %>
  131. <pre data-language="javascript"><%= example %></pre>
  132. <% end %>
  133. <% end %>
  134. <div class="_attribution">
  135. <p class="_attribution-p">
  136. <%= options[:attribution] %>
  137. <br>
  138. <a href="<%= self.base_url %><%= feature_id %>" class="_attribution-link">
  139. <%= self.base_url %><%= feature_id %>
  140. </a>
  141. </p>
  142. </div>
  143. HTML
  144. def get_latest_version(opts)
  145. get_latest_github_release('date-fns', 'date-fns', opts)
  146. end
  147. end
  148. end