refactor
This commit is contained in:
		
							parent
							
								
									375a3b8463
								
							
						
					
					
						commit
						acae10d185
					
				
							
								
								
									
										43
									
								
								publish.py
								
								
								
								
							
							
						
						
									
										43
									
								
								publish.py
								
								
								
								
							| 
						 | 
				
			
			@ -18,6 +18,8 @@ htmltemplate = """<!DOCTYPE html>
 | 
			
		|||
</head>
 | 
			
		||||
<body>{body}</body></html>"""
 | 
			
		||||
 | 
			
		||||
settingsfile = "../textes/settings.yml"
 | 
			
		||||
 | 
			
		||||
def apply_html_template(body, title):
 | 
			
		||||
    return htmltemplate.format(body=body, title=title)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +34,7 @@ def md2gmi(md):
 | 
			
		|||
    gmi = re.sub(r'\*(.*)\*', r'\1', gmi)
 | 
			
		||||
    return gmi
 | 
			
		||||
 | 
			
		||||
with open("settings.yml", "r", encoding="utf-8") as file:
 | 
			
		||||
with open(settingsfile, "r", encoding="utf-8") as file:
 | 
			
		||||
    index = yaml.safe_load(file)
 | 
			
		||||
 | 
			
		||||
htmlhome = "<h1>{}</h1><p>{}</p>".format(index['title'], index['description'])
 | 
			
		||||
| 
						 | 
				
			
			@ -42,12 +44,21 @@ os.makedirs("out/html", exist_ok=True)
 | 
			
		|||
os.makedirs("out/gmi", exist_ok=True)
 | 
			
		||||
 | 
			
		||||
for folderinfo in index['folders']:
 | 
			
		||||
    htmlhome += "\n<h2>{}</h2>\n<ul>".format(folderinfo['title'])
 | 
			
		||||
    gmihome += "\n\n## {}".format(folderinfo['title'])
 | 
			
		||||
 | 
			
		||||
    if 'texts' in folderinfo:
 | 
			
		||||
        for textinfo in folderinfo['texts']:
 | 
			
		||||
            with open(os.path.join(folderinfo['folder'], textinfo['file']), "r", encoding="utf-8") as mdfile:
 | 
			
		||||
    sectiontitle = folderinfo['title']
 | 
			
		||||
 | 
			
		||||
    htmlhome += "\n<h2>{}</h2>\n<ul>".format(sectiontitle)
 | 
			
		||||
    gmihome += "\n\n## {}".format(sectiontitle)
 | 
			
		||||
 | 
			
		||||
    for fileinfo in folderinfo['files']:
 | 
			
		||||
 | 
			
		||||
        filename = fileinfo['file']
 | 
			
		||||
        filetitle = fileinfo['title']
 | 
			
		||||
        fileroot, fileextension = os.path.splitext(filename)
 | 
			
		||||
 | 
			
		||||
        if fileextension == '.md':
 | 
			
		||||
 | 
			
		||||
            with open(os.path.join(folderinfo['folder'], filename), "r", encoding="utf-8") as mdfile:
 | 
			
		||||
                mdcontent = mdfile.read()
 | 
			
		||||
 | 
			
		||||
            # clean header
 | 
			
		||||
| 
						 | 
				
			
			@ -56,20 +67,20 @@ for folderinfo in index['folders']:
 | 
			
		|||
            htmlcontent = md2html(mdcontent)
 | 
			
		||||
            gmicontent = md2gmi(mdcontent)
 | 
			
		||||
 | 
			
		||||
            with open("out/html/{}".format(textinfo['file']).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile:
 | 
			
		||||
                htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(textinfo['title'], index['title'])))
 | 
			
		||||
            with open("out/html/{}".format(filename).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile:
 | 
			
		||||
                htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(filetitle, index['title'])))
 | 
			
		||||
 | 
			
		||||
            with open("out/gmi/{}".format(textinfo['file']).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile:
 | 
			
		||||
            with open("out/gmi/{}".format(filename).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile:
 | 
			
		||||
                gmifile.write(gmicontent)
 | 
			
		||||
 | 
			
		||||
            if 'restricted' not in textinfo:
 | 
			
		||||
                htmlhome += "<li><a href=\"{}\">{}</a></li>".format(textinfo['file'].replace('.md', '.html'), textinfo['title'])
 | 
			
		||||
                gmihome += "\n=> {} {}".format(textinfo['file'].replace('.md', '.gmi').replace(' ', '%20'), textinfo['title'])
 | 
			
		||||
            htmlhome += "<li><a href=\"{}\">{}</a></li>".format(filename.replace('.md', '.html'), filetitle)
 | 
			
		||||
            gmihome += "\n=> {} {}".format(filename.replace('.md', '.gmi').replace(' ', '%20'), filetitle)
 | 
			
		||||
 | 
			
		||||
    if 'sounds' in folderinfo:
 | 
			
		||||
        for soundinfo in folderinfo['sounds']:
 | 
			
		||||
            shutil.copy(os.path.join(folderinfo['folder'], soundinfo['file']), 'out/html')
 | 
			
		||||
            htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(soundinfo['title'], soundinfo['file'])
 | 
			
		||||
        else:
 | 
			
		||||
 | 
			
		||||
            # sounds
 | 
			
		||||
            shutil.copy(os.path.join(folderinfo['folder'], filename), 'out/html')
 | 
			
		||||
            htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(filetitle, filename)
 | 
			
		||||
 | 
			
		||||
    htmlhome += "\n</ul>"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,19 +3,19 @@ description: Description of my web site
 | 
			
		|||
folders:
 | 
			
		||||
  - folder: ../path/containing/texts
 | 
			
		||||
    title: Section title
 | 
			
		||||
    texts:
 | 
			
		||||
    files:
 | 
			
		||||
        - file: file1.md
 | 
			
		||||
          title: Title 1
 | 
			
		||||
        - file: file2.md
 | 
			
		||||
          title: Title 2
 | 
			
		||||
  - folder: ../path/containing/texts
 | 
			
		||||
    title: Other section title
 | 
			
		||||
    texts:
 | 
			
		||||
    files:
 | 
			
		||||
      - file: file3.md
 | 
			
		||||
        title: Title 3
 | 
			
		||||
  - folder: ../path/containing/sounds
 | 
			
		||||
    title: Sound section title
 | 
			
		||||
    sounds:
 | 
			
		||||
    files:
 | 
			
		||||
      - file: song1.mp3
 | 
			
		||||
        title: Song 1
 | 
			
		||||
      - file: song 2.mp3
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue