publi.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # @Author: Bachir Soussi Chiadmi <bach>
  4. # @Date: 11-04-2017
  5. # @Email: bachir@figureslibres.io
  6. # @Last modified by: bach
  7. # @Last modified time: 18-04-2017
  8. # @License: GPL-V3
  9. import sys, os, getopt
  10. import markdown
  11. import re
  12. import markdown
  13. import pypandoc
  14. print("OLA#5 publication generator")
  15. output_md = '<div class="page">\n'
  16. output_md += "# OLA5 Algolittéraire Publication\n\n"
  17. output_md += '</div>\n'
  18. content_dir = '../content'
  19. for d in os.listdir(content_dir):
  20. if not d.startswith('.') and d != 'model':
  21. pres_path = os.path.join(content_dir, d, "publi.textile")
  22. if os.path.isfile(pres_path):
  23. print('\n**',d,'**')
  24. pres_f = open(pres_path)
  25. pres_txt = pres_f.read()
  26. pres_f.close()
  27. # print(pres_txt)
  28. meta_match = re.match(r'---(.*)---', pres_txt, re.DOTALL)
  29. # meta = meta_match.group(0)
  30. if meta_match:
  31. output_md += '<div class="page">\n'
  32. output_md += '<div class="info">\n'
  33. output_md += '<div class="info_left">\n'
  34. meta = meta_match.group(0)
  35. author_match = re.search(r'Author: (.+)', meta)
  36. author = author_match.group(1) if author_match else "missing"
  37. # print('author:', author)
  38. title_match = re.search(r'Title: (.+)', meta)
  39. title = title_match.group(1) if title_match else "missing"
  40. # print('title:', title)
  41. src_match = re.search(r'Src: (.+)', meta)
  42. src = src_match.group(1) if src_match else "missing"
  43. print('src:', src)
  44. pres = pres_txt.replace(meta_match.group(0), '').strip()
  45. # print('pres:',pres)
  46. output_md += "\n\n## "+title
  47. output_md += "\n\n### "+author
  48. output_md += '\n\n</div>\n'
  49. output_md += '<div class="info_right">\n'
  50. output_md += "\n\n*"+pres+"*"
  51. output_md += '\n\n</div>\n'
  52. output_md += '\n\n</div>\n'
  53. export_path = os.path.join(content_dir,d, src)
  54. print('export_path:', export_path)
  55. if os.path.isfile(export_path):
  56. with open(export_path, 'r', encoding="utf8") as export_f:
  57. output_md += "\n\n"+export_f.read()
  58. output_md += '\n\n</div>\n'
  59. # print('output_md:\n', output_md)
  60. with open('publi.md', 'w') as md_f:
  61. md_f.write(output_md)
  62. pdoc_args = ['--mathjax']
  63. pdoc_filters = []
  64. output_html = pypandoc.convert('publi.md',
  65. to='html5',
  66. format='markdown+header_attributes+link_attributes+bracketed_spans',
  67. extra_args=pdoc_args,
  68. filters=pdoc_filters)
  69. # outputfile=out_f)
  70. # print(output_html)
  71. index_html = "<html>\n \
  72. \t<head>\n \
  73. \t\t<meta charset='utf-8'>\n"
  74. index_html += "<link rel='stylesheet' href='/fonts/cmu-Serif/cmun-serif.css'>"
  75. with open('css/main.css', 'r', encoding="utf8") as css_f:
  76. index_html += "\n\n"+css_f.read()
  77. index_html += "\t</head>\n \
  78. \t<body>\n \
  79. \t\t<div id='root'>"
  80. index_html += output_html
  81. index_html += "\n</div>\n</body>\n \
  82. </html>"
  83. with open('index.html', 'w') as html_f:
  84. html_f.write(index_html)