import os from datetime import datetime # Requires pandoc to work! # read in the configuration file for use by the script conf = dict() conf['theme']='klab1' # temporary solution until I implement a config file conf['site name']='KLAB' # :: on start, ensure certain directories etc exist, if not, create them. if os.path.isdir('./_site') == False: os.mkdir('./_site') # add options for deploying the whole script, so need zipped config file, and theme directory. # :: make sure the templates exist in pandoc template dirs. def new_page(): os.system('clear') page_name = raw_input('page name (no .md required), start with \":\" for a year-dir, otherwise it assumes root-dir:\n> ').rstrip() + '.md' # :: decide on directory structure (page_place) & format page_name page_place = './_site/' page_name = page_name.replace(' ','-') if ":" in page_name: page_name = page_name.replace(':','') if '.md' not in page_name: page_name = str(page_name) + '.md' page_place = page_place + str(datetime.now().strftime('%Y')) + '/' # add year to the mix page_place = page_place + str(datetime.now().strftime('%j%H%M%S')) + '/' # UUID component: Day, hour, minute, second # :: make the directory if os.path.isdir(str(page_place)) == False: os.system('mkdir -p ' + str(page_place)) # :: create the file tmp = str(page_place) + str(page_name) with open(tmp ,'w+') as of: of.write('\n\n
') of.write('# TITLE\n\n\n\n
') # :: open the file for editing & directory for stuff os.system('xdg-open ' + str(tmp) + ' & xdg-open ' + str(page_place)) # :: give the link if I want to update another page with it print('\n\nMD LINK: [...](/' + page_place.replace('./_site','') + page_name.replace('.md','.html') + ')\n\n\n') def edit_page(): # :: dmenu, get the page name by listing all '.md' files in dir/subdir of ./_site/ page = os.popen('find ./_site -type f | dmenu -l 5 -i -p \"SSG>EDIT> \"').read().rstrip() # :: open the markdown file in editor and open the directory os.system('xdg-open "' + page + '" & xdg-open "' + os.path.dirname(str(page)) + '" &') def open_dir(): page = os.popen('find ./_site -type d | dmenu -l 5 -i -p \"SSG>OPEN_DIR> \"').read().rstrip() # :: open the directory os.system('xdg-open "' + os.path.dirname(str(page)) + '"') def generate(): # :: delete the old file just in case if os.path.isfile('.tmp_file_list') == True: os.system('rm .tmp_file_list') # :: get a list of all markdown files in the _site directory, store in a temp file os.system('find ./_site -type f -name "*.md" > .tmp_file_list') # :: open the temp file list and start going through it line by line with open('.tmp_file_list', 'r') as flist: for line in flist: # :: set the generate flag generate_flag = False # :: format the line, remove new line, get checksum filename. line = line.rstrip() line = line.replace('\n','') # :: csum is same name as the markdown file but with a . in front and .csum at the end (file.md = .file.md.csum) csum_file = os.path.dirname(line) + '/' + '.' + os.path.basename(line) + '.csum' # :: if there is no matching checksum file found in the directory, make one and generate the content. if os.path.isfile(csum_file) == False: os.system('cksum ' + line + ' > ' + csum_file) generate_flag = True else: # :: calculate the new checksum new_cksum = os.popen('cksum ' + line).read() # :: get the old checksum with open(csum_file,'r') as of: old_cksum = of.readline() # :: compare the two checksums if new_cksum not in old_cksum: generate_flag = True # :: delete the old checksum file, and enter the new checksum values into the file. os.system('rm ' + csum_file) with open(csum_file,'w+') as of: of.write(new_cksum) # :: check generate flag, generate if it is True if generate_flag == True: # :: do the generation of MARKDOWN >> HTML with open(line.replace('.md','.html'),'w+') as html_file: theme_file = open('./themes/' + str(conf['theme']) + '/page.html', 'r') for theme_file_line in theme_file: # :: all themevariables are marked using $$ if '$$' not in theme_file_line: html_file.write(theme_file_line) elif '$$CONTENT' in theme_file_line: # :: tag means content of the markdown file / uses pandoc os.system('pandoc ' + str(line) + ' -o .tmp_md2html') with open('.tmp_md2html', 'r') as content_file: for content_file_line in content_file: html_file.write(str(content_file_line)) os.system('rm .tmp_md2html') elif '$$SITENAME' in theme_file_line: html_file.write(theme_file_line.replace('$$SITENAME',str(conf['site name']))) # :: delete the temporary file os.system('rm .tmp_file_list') def backup(): print('NYI') # :: does a backup of the whole site, and keeps it for X past (config) # :: a tar/zip archive. excludes the backup dir, but nothing else. So is site and script and themes and ... # MAIN MENU HERE! def main_menu(): while 1: os.system('clear') print('SSG (v0.3)\n') m_choice = str(raw_input('(N)ew page\n(E)dit page\n(O)pen directory\n(G)enerate\n(Q)uit\n=========\nCHOICE> ')) if len(m_choice) == 1: m_choice = m_choice.upper() if m_choice == "Q": exit() elif m_choice == "G": generate() elif m_choice == "N": new_page() elif m_choice == "E": edit_page() elif m_choice == "O": open_dir() else: m_choice = raw_input('ERROR! that is not a valid choice! Press return.') else: m_choice = raw_input('ERROR! that is not a valid choice! Press return.') # Future DMENU system replacement! main_menu() ''' NOTES: - Pure genereative for now. - Upload is handled manually. - '''