Guest User

Untitled

a guest
Oct 22nd, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import json
  2. import urllib2
  3. import re
  4.  
  5. img_pattern = "[[File:\\2.png|frame|none|none|\\1]]"
  6. img_reg_pattern = re.compile("!\\[(.*)\\]\\(.*:(.*)\\)")
  7.  
  8. title_pattern = "== \\1 =="
  9. title_reg_pattern = re.compile("# (.*)")
  10.  
  11. section_pattern = "=== \\1 ==="
  12. section_reg_pattern = re.compile("## (.*)")
  13.  
  14. link_pattern = "[[\\2|\\1]]"
  15. link_reg_pattern = re.compile("\\[(.*)\\]\\((?:.*/)?(.*).md\\)")
  16.  
  17. doc_list = {"items": {}, "blocks": {}}
  18.  
  19. def parse_doc(contents):
  20.     doc = img_reg_pattern.sub(img_pattern, contents)
  21.     doc = section_reg_pattern.sub(section_pattern, doc)
  22.     doc = title_reg_pattern.sub(title_pattern, doc)
  23.     doc = link_reg_pattern.sub(link_pattern, doc)
  24.     doc = doc.replace("@", "-")
  25.     return doc
  26.  
  27. itemdoc_json = json.loads( urllib2.urlopen("https://api.github.com/repositories/17380796/contents/src/main/resources/assets/computronics/doc/computronics/en_US/item").read() )
  28. blockdoc_json = json.loads( urllib2.urlopen("https://api.github.com/repositories/17380796/contents/src/main/resources/assets/computronics/doc/computronics/en_US/item").read() )
  29.  
  30. for entry in itemdoc_json:
  31.     if entry['download_url'] and entry['name']:
  32.         doc_content = urllib2.urlopen( entry['download_url'] ).read()
  33.         doc_list['items'][entry['name']] = parse_doc(doc_content)
  34.  
  35. for entry in blockdoc_json:
  36.     if entry['download_url'] and entry['name']:
  37.         doc_content = urllib2.urlopen( entry['download_url'] ).read()
  38.         doc_list['blocks'][entry['name']] = parse_doc(doc_content)
  39.        
  40. # Do whatever you need to do with it here, I'll just print it out
  41. print doc_list
Advertisement
Add Comment
Please, Sign In to add comment