Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-doc
Path: blob/main/website/tools/hardware-notes-processor.rb
18081 views
1
#!/usr/bin/env ruby
2
=begin
3
4
BSD 2-Clause License
5
Copyright (c) 2020-2026, The FreeBSD Project
6
Copyright (c) 2020-2026, Sergio Carlavilla <[email protected]>
7
8
This script will generate the hardware notes
9
10
=end
11
require 'open3'
12
13
# Main method
14
puts "Processing hardware notes..."
15
16
if ARGV.length < 1 || ARGV.length > 1
17
puts "Only expecting the release version"
18
exit
19
end
20
21
# Extract FreeBSD version from newvers.sh
22
freebsd_version = nil
23
newvers_path = './tmp/sys/conf/newvers.sh'
24
if File.exist?(newvers_path)
25
File.foreach(newvers_path) do |line|
26
if line =~ /REVISION="([^"]+)"/
27
freebsd_version = $1
28
break
29
end
30
end
31
end
32
33
if freebsd_version.nil?
34
puts "WARNING: Could not find FreeBSD version in #{newvers_path}"
35
freebsd_version = 'X.Y'
36
end
37
38
hardwareNotesPath = ARGV[0]
39
hardwareNotesContent = ""
40
41
File.foreach(hardwareNotesPath).with_index do |original_line, idx|
42
line = original_line.dup # allow modification
43
44
# Replace version placeholders in the first 10 lines
45
if idx < 10
46
line.gsub!(/X\.0/, freebsd_version)
47
line.gsub!(/X\.Y/, freebsd_version)
48
end
49
50
if (line[/&hwlist.\b/])
51
manualPage = line.gsub("&hwlist.", "").gsub(";", "").gsub("\n", "")
52
53
if(File.exist?("tmp/share/man/man4/" + manualPage + ".4"))
54
cmd = "mandoc -Tmarkdown tmp/share/man/man4/" + manualPage + ".4 | sed -n '/^# HARDWARE/,/^# /{ /^# /d; p; }'"
55
mandocOut, err, s = Open3::capture3(cmd)
56
if s.success?
57
#replace \_ to _ and \[ to [ in drivers name and description
58
mandocOut.gsub!(/\\_/, '_')
59
mandocOut.gsub!(/\\\[/, '[')
60
61
# extract Nm (real driver name)
62
nm_cmd = "grep -m1 '^\\.Nm' tmp/share/man/man4/#{manualPage}.4"
63
nmOut, err2, s2 = Open3.capture3(nm_cmd)
64
65
if s2.success?
66
67
nm = nmOut.split[1]
68
69
if nm && !nm.empty?
70
driverName = File.basename(manualPage)
71
72
if nm != driverName
73
# f.e man ar40xx has driver name ar40xx_switch
74
man_link = "link:https://man.freebsd.org/cgi/man.cgi?query=#{driverName}&amp;apropos=&amp;&sektion=4&amp;format=html[#{nm}]"
75
else
76
man_link = "man:#{driverName}[4]"
77
end
78
# replace only first occurrence, preserving markdown formatting
79
mandocOut.sub!(/[*_]*#{nm}[*_]*/) do
80
man_link
81
end
82
end
83
end
84
85
if mandocOut.strip.empty?
86
puts "WARNING: No HARDWARE section content for manual page #{manualPage}"
87
next
88
end
89
90
hardwareNotesContent << mandocOut
91
else
92
puts "WARNING: The manual page " + manualPage + " without HARDWARE exists or malformed"
93
end
94
else
95
puts "WARNING: The manual page " + manualPage + " does not exists"
96
end
97
else
98
hardwareNotesContent << line
99
end
100
end
101
102
File.open(hardwareNotesPath, 'w') do |out|
103
out << hardwareNotesContent
104
end
105
106