Skip to content

Commit

Permalink
Re #6897. Parsing image information in from the wiki docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Apr 23, 2013
1 parent 95b6d75 commit ff6257b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/docs/qtassistant/algorithm_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def process_algorithm(name, versions, qhp, outputdir, **kwargs): # was (args, al
#htmlfile.p(alg.getWikiSummary())
wiki = MediaWiki(htmlfile)
wiki.parse(alg.getWikiSummary())
for img in wiki.images:
qhp.addFile(os.path.join(HTML_DIR, "img", img))

htmlfile.h3("Usage")
text = wiki_tools.create_function_signature(alg, name)
Expand Down
2 changes: 2 additions & 0 deletions Code/Mantid/docs/qtassistant/fitfunctions_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def process_function(name, qhp, outputdir, **kwargs): # was (args, algo):
htmlfile.h3("Summary")
wiki = MediaWiki(htmlfile)
wiki.parse(wiki_tools.get_fitfunc_summary(name, False))
for img in wiki.images:
qhp.addFile(os.path.join(HTML_DIR, "img", img))

if func.numParams() <= 0:
htmlfile.h3("No Parameters")
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/docs/qtassistant/htmlwriter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class HtmlWriter:
def __init__(self, filename, title=None):
self.name = filename
self.__handle = open(filename, 'w')
self.__tagstack = []
self.openTag("html")
Expand Down
71 changes: 69 additions & 2 deletions Code/Mantid/docs/qtassistant/mediawiki.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
from assistant_common import WEB_BASE, HTML_DIR, addEle, addTxtEle
import re

def formatImgHtml(raw):
#print "RAW:", raw

# cleanup the tag from the text
index = raw.index(':') # look for first ':'
if index < 0:
index = 0
else:
index += 1
raw = raw[index:-2]

#print "RAW:", raw

# chop tag into something more workable
components = raw.split('|')
img = components[0] # image filename is always first

# get the other bits of meta-data
alt = None
caption = None
for item in components[1:]:
item_low = item.lower()
if item_low == "thumb":
pass
elif item.endswith('px'):
pass
elif item_low == "right" or item_low == "center":
pass
elif item_low.startswith("alt"):
alt = '='.join(item.split('=')[1:])
else:
caption = item


html = "<figure>"
html += "<img src='img/" + img + "'"
if alt is not None:
html += " alt='%s'" % alt
html += "/>"
if caption is not None:
html += "\n<figcaption>%s</figcaption>\n" % caption
html += "</figure>\n"
#print "HTML:", html
return (img, html)

class MediaWiki:
def __init__(self, htmlfile):
self.__file = htmlfile
self.__types = []
self.images = []

def __parseImgs(self, text):
# Get all of the raw image links
raw_img = re.findall(r'\[\[Image:.*\]\]', text, flags=re.MULTILINE)
raw = re.findall(r'\[\[File:.*\]\]', text, flags=re.MULTILINE)
raw.extend(raw_img)

# generate the html
html = []
for src in raw:
(imagefile, newtxt) = formatImgHtml(src)
self.images.append(imagefile)
html.append(newtxt)

for (orig, repl) in zip(raw, html):
#print ">>>", orig
#print "<<<", repl
text = text.replace(orig, repl)

return text.strip()

def __clearEmpty(self, text):
result = []
Expand Down Expand Up @@ -59,7 +126,6 @@ def __fixUL(self, text):
return "\n".join(text)

def __fixHEADERS(self, text):
import re
results = re.findall(r'\s+==.*==\s+', text)
for item in results:
text = text.replace(item, "\n\n"+item.strip()+"\n\n")
Expand Down Expand Up @@ -101,6 +167,7 @@ def parse(self, text):
text = text.strip()
if len(text) <= 0:
return # don't bother if it is empty
text = self.__parseImgs(text)
#print "01>>>", text, "<<<"
if text.startswith("== Deprecation notice =="):
stuff = "== Deprecation notice =="
Expand Down Expand Up @@ -135,7 +202,7 @@ def parse(self, text):
if annotate == "blank":
if not text[i-1].startswith("<h"):
self.__file.write("</p>\n")
if not text[i+1].startswith("<h"):
if i+1 < num_lines and not text[i+1].startswith("<h"):
self.__file.write("<p>")
else:
self.__file.write(annotate)
Expand Down

0 comments on commit ff6257b

Please sign in to comment.