publi.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. meta = meta_match.group(0)
  33. author_match = re.search(r'Author: (.+)', meta)
  34. author = author_match.group(1) if author_match else "missing"
  35. # print('author:', author)
  36. title_match = re.search(r'Title: (.+)', meta)
  37. title = title_match.group(1) if title_match else "missing"
  38. # print('title:', title)
  39. src_match = re.search(r'Src: (.+)', meta)
  40. src = src_match.group(1) if src_match else "missing"
  41. print('src:', src)
  42. pres = pres_txt.replace(meta_match.group(0), '').strip()
  43. # print('pres:',pres)
  44. output_md += "\n\n## "+title
  45. output_md += "\n\n### "+author
  46. output_md += "\n\n*"+pres+"*"
  47. export_path = os.path.join(content_dir,d, src)
  48. print('export_path:', export_path)
  49. if os.path.isfile(export_path):
  50. print('EXPORT OK')
  51. with open(export_path, 'r', encoding="utf8") as export_f:
  52. output_md += "\n\n"+export_f.read()
  53. output_md += '</div>\n'
  54. # print('output_md:\n', output_md)
  55. with open('publi.md', 'w') as md_f:
  56. md_f.write(output_md)
  57. pdoc_args = ['--mathjax']
  58. pdoc_filters = []
  59. output_html = pypandoc.convert('publi.md',
  60. to='html5',
  61. format='markdown+header_attributes+link_attributes+bracketed_spans',
  62. extra_args=pdoc_args,
  63. filters=pdoc_filters)
  64. # outputfile=out_f)
  65. # print(output_html)
  66. index_html = "<html>\n \
  67. \t<head>\n \
  68. \t\t<meta charset='utf-8'>\n"
  69. with open('css/main.css', 'r', encoding="utf8") as css_f:
  70. index_html += "\n\n"+css_f.read()
  71. index_html += "\t</head>\n \
  72. \t<body>\n \
  73. \t\t<div id='root'>"
  74. index_html += output_html
  75. index_html += "\n</div>\n</body>\n \
  76. </html>"
  77. with open('index.html', 'w') as html_f:
  78. html_f.write(index_html)