Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created December 26, 2019 01:31
Show Gist options
  • Save phwelo/677714321bd51e2e177f31dbb2b48a8b to your computer and use it in GitHub Desktop.
Save phwelo/677714321bd51e2e177f31dbb2b48a8b to your computer and use it in GitHub Desktop.
asdff
#!/usr/bin/env python3
import os
import markdown
from subprocess import Popen, DEVNULL
from rofi import Rofi
r = Rofi()
# Path to data docs
data_dir = 'terraform-provider-aws/website/docs/d'
browser = 'firefox'
# Path to resource docs
resource_dir = 'terraform-provider-aws/website/docs/r'
def process_file(path):
file_obj = {}
file = open(path)
file_obj['contents'] = file.readlines()
file_obj['page_title'] = file_obj['contents'][3].split('AWS: ')[1][0:-2].replace('aws_','')
file_obj['page_desc'] = file_obj['contents'][5].lstrip()
return file_obj
def dump_contents(path):
file = open(path)
return file.read()
def sweep_directory(scan_path):
rofi_table = []
for filename in os.listdir(scan_path):
if '.html.markdown' in filename:
process_results = process_file(scan_path + '/' + filename)
rofi_table.append({'title': process_results['page_title'], 'description': process_results['page_desc'], 'raw': dump_contents(scan_path + '/' + filename)})
return(rofi_table)
def generate_rofi_menu(list_of_dicts):
rofi_list = []
for entry in list_of_dicts:
rofi_list.append(entry['title'] + ' - ' + entry['description'])
index, key = r.select('Terraform search?', rofi_list, rofi_args=['-matching normal'])
return index
def add_style(css_path, body):
style_file = open('./style.css')
style_css = '<style>' + style_file.read() + '</style>'
return style_css + html_body
aws_data_table = sweep_directory(data_dir)
aws_resource_table = sweep_directory(resource_dir)
combined_list = aws_resource_table + aws_data_table
selected_index = generate_rofi_menu(combined_list)
html_body = markdown.markdown(combined_list[selected_index]['raw'])
html_complete = add_style('./style.css', html_body)
f = open("/tmp/tfap.html", "w")
f.write(html_complete)
f.close()
Popen(['nohup', browser, '/tmp/tfap.html'], stdout=DEVNULL, stderr=DEVNULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment