|
| 1 | +module Docs |
| 2 | + class Powershell < FileScraper |
| 3 | + self.name = 'PowerShell' |
| 4 | + self.type = 'simple' |
| 5 | + self.root_path = 'Microsoft.PowerShell.Core/Get-Help.html' |
| 6 | + self.links = { |
| 7 | + home: 'https://learn.microsoft.com/powershell', |
| 8 | + code: 'https://github.com/MicrosoftDocs/PowerShell-Docs' |
| 9 | + } |
| 10 | + html_filters.push 'powershell/clean_html', 'powershell/entries' |
| 11 | + text_filters.replace 'attribution', 'powershell/attribution' |
| 12 | + |
| 13 | + # https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/LICENSE-CODE.md |
| 14 | + # https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/LICENSE.md |
| 15 | + options[:attribution] = <<-HTML |
| 16 | + © Microsoft Corporation |
| 17 | + Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0. |
| 18 | + HTML |
| 19 | + |
| 20 | + version '7.7' do |
| 21 | + self.release = '7.7' |
| 22 | + end |
| 23 | + |
| 24 | + version '7.6' do |
| 25 | + self.release = '7.6' |
| 26 | + end |
| 27 | + |
| 28 | + version '7.5' do |
| 29 | + self.release = '7.5' |
| 30 | + end |
| 31 | + |
| 32 | + version '7.4' do |
| 33 | + self.release = '7.4' |
| 34 | + end |
| 35 | + |
| 36 | + version '5.1' do |
| 37 | + self.release = '5.1' |
| 38 | + end |
| 39 | + |
| 40 | + version 'Scripting' do |
| 41 | + self.root_path = 'discover-powershell.html' |
| 42 | + |
| 43 | + def source_directory |
| 44 | + @source_directory ||= File.join(Docs::FileScraper::SOURCE_DIRECTORY, 'powershell', 'docs-conceptual') |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + def source_directory |
| 49 | + @source_directory ||= File.join(Docs::FileScraper::SOURCE_DIRECTORY, 'powershell', self.class.version) |
| 50 | + end |
| 51 | + |
| 52 | + def get_latest_version(opts) |
| 53 | + # Stable PowerShell releases are tagged like "v7.5.2"; the docs are |
| 54 | + # versioned by major.minor (e.g. "7.5"), so drop the patch component. |
| 55 | + get_latest_github_release('PowerShell', 'PowerShell', opts).split('.')[0..1].join('.') |
| 56 | + end |
| 57 | + |
| 58 | + # No index page, enumerate all HTML files |
| 59 | + def request_all(urls) |
| 60 | + assert_source_directory_exists |
| 61 | + Dir.glob(File.join(source_directory, '**', '*.md')).sort.each do |path| |
| 62 | + url = File.join(base_url.to_s, path.sub("#{source_directory}/", '')) |
| 63 | + yield request_one(url) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + def parse(response) |
| 70 | + body = response.body.sub(/\A---\s*\n.*?\n---\s*\n/m, '') |
| 71 | + html = markdown_renderer.render(body) |
| 72 | + [Parser.new("<html></head><body>#{html}</body></html>").html, ""] |
| 73 | + end |
| 74 | + |
| 75 | + def markdown_renderer |
| 76 | + require 'redcarpet' |
| 77 | + @markdown_renderer ||= Redcarpet::Markdown.new( |
| 78 | + Redcarpet::Render::HTML.new(with_toc_data: true), |
| 79 | + autolink: true, |
| 80 | + fenced_code_blocks: true, |
| 81 | + no_intra_emphasis: true, |
| 82 | + tables: true |
| 83 | + ) |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments