PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/journal.py

https://github.com/imrehg/website
Python | 83 lines | 80 code | 2 blank | 1 comment | 0 complexity | 2821fae4d9fd297f5e8ab100453e4b7d MD5 | raw file
  1. #!/usr/bin/env python
  2. import sys
  3. import re
  4. HEADER = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5. <html>
  6. <head>
  7. <link rel="shortcut icon" type="image/x-icon" href="../img/favicon.ico" />
  8. <link href="../css/bootstrap/bootstrap.css" rel="stylesheet" />
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  10. <link href="../css/bootstrap/bootstrap-responsive.css" rel="stylesheet" />
  11. <link rel="stylesheet" type="text/css" href="../css/swc.css" />
  12. <link rel="stylesheet" type="text/css" href="../css/swc-bootstrap.css" />
  13. <link rel="alternate" type="application/rss+xml" title="The Software Carpentry Blog RSS Feed" href="/tmp/swc/feed.xml"/>
  14. <!-- override article title centering -->
  15. <style type="text/css">
  16. div.title {
  17. text-align: left;
  18. padding: 10px 40px;
  19. }
  20. </style>
  21. <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
  22. <!--[if lt IE 9]>
  23. <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  24. <![endif]-->
  25. <title>Software Carpentry Blog</title>
  26. </head>
  27. <body>
  28. <div class="container">
  29. <div class="banner">
  30. <a href="../index.html" title="Software Carpentry Home">
  31. <img src="../img/software-carpentry-banner.png" alt="Software Carpentry banner" />
  32. </a>
  33. </div>
  34. """
  35. ENTRY = """
  36. <div class="title">
  37. <h2>%(title)s</h2>
  38. </div>
  39. <div class="row-fluid">
  40. <div class="span10 offset1">
  41. %(body)s
  42. </div>
  43. </div>
  44. """
  45. FOOTER = """
  46. </div>
  47. </body>
  48. </html>
  49. """
  50. Post_Id_Pat = re.compile('<meta\s+name="post_id"\s+content="([^"]+)"\s*/>')
  51. Journal_Pat = re.compile('<meta\s+name="journal"\s+content="([^"]+)"\s*/>')
  52. Title_Pat = re.compile('<h1>(.+)</h1>')
  53. Body_Pat = re.compile('<div class="span10 offset1">(.+)<div id="disqus_thread">', re.DOTALL)
  54. #----------------------------------------
  55. def extract(filename):
  56. with open(filename, 'r') as reader:
  57. data = reader.read()
  58. m = Journal_Pat.search(data)
  59. if (not m) or (m.group(1).lower() != 'true'):
  60. return None
  61. post_id = int(Post_Id_Pat.search(data).group(1))
  62. title = Title_Pat.search(data).group(1)
  63. body = Body_Pat.search(data).group(1).strip().replace('../../..', '..')
  64. return (post_id, ENTRY % {'title' : title, 'body' : body})
  65. #----------------------------------------
  66. if __name__ == '__main__':
  67. assert len(sys.argv) > 1, 'Need at least one post filename'
  68. print(HEADER)
  69. entries = [extract(filename) for filename in sys.argv[1:]]
  70. entries = [e for e in entries if e]
  71. entries.sort()
  72. for e in entries:
  73. print(e[1])
  74. print(FOOTER)