aboutsummaryrefslogtreecommitdiff
path: root/website/tools/hardware-notes-creator.py
blob: 67c27ccb7d12468632038a3f96f6001a1d8d3162 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
BSD 2-Clause License

Copyright (c) 2020-2021, The FreeBSD Project
Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>

This script will generate the Table of Contents of the Handbook
"""
#!/usr/bin/env python3

import sys, getopt
import ntpath
import subprocess
import os

manPagesPath = ""

def loadManPages(manPagesDir):
  manPagesPaths = []

  for root, _, files in os.walk(manPagesDir):
    for file in files:
      if file.endswith(".4"):
        manPagesPaths.append(os.path.join(root, file))
  return manPagesPaths

def createHardwareNotesDirectory(path):
  try:
    os.mkdir(path)
  except OSError:
    print ("Creation of the directory %s failed" % path)
  else:
    print ("Successfully created the directory %s " % path)

def main(argv):

  try:
    opts, args = getopt.getopt(argv,"hp:",["path="])
  except getopt.GetoptError:
    print('hardware-notes-creator.py -p <path>')
    sys.exit(2)

  for opt, arg in opts:
    if opt == '-h':
      print('hardware-notes-creator.py -p <path>')
      sys.exit()
    elif opt in ("-p", "--path"):
      manPagesPath = arg

  manPages = loadManPages("/home/carlavilla/Projects/base/src/share/man/man4/")
  createHardwareNotesDirectory('/home/carlavilla/Projects/testsssss')

  for manPage in manPages:
    manPageParsed = subprocess.getoutput("mandoc -Tmarkdown " + manPage + " | sed -n '/# HARDWARE/,/#/{/#/!p;}'")

    if len(manPageParsed) > 0:
      manPageName = ntpath.basename(manPage).replace(".4", "")

      with open('/home/carlavilla/Projects/testsssss/{}.adoc'.format(manPageName), 'w', encoding = 'utf-8') as manPageFile:
        manPageFile.write(manPageParsed)

if __name__ == "__main__":
  main(sys.argv[1:])