######################################################################## # HyperTIES to XML Converter # By Don Hopkins, 2/4/2005 ######################################################################## ######################################################################## # Imports import sys, os, stat, types, StringIO from XMLUtilities import * ######################################################################## # Global Definitions TiesRoot = '/home/sites/DonHopkins.com/home/ties' HRefRoot = "hyperties://" DatabaseNames = ( 'newdb', 'doc', 'global', 'cookbook', 'emacs', 'news', 'newsdoc', 'cam', ) ######################################################################## # Storyboard Compiler def ConvertDatabases( out, doMasterIndex=True, doStoryboards=True, doPictures=True, doTargets=True): print >>out, "" masterIndex = { 'storyboards': {}, 'pictures': {}, 'targets': {}, } for databaseName in DatabaseNames: databasePath = os.path.join(TiesRoot, databaseName) IndexDatabase( TiesRoot, databaseName, databasePath, masterIndex, out) print >>out, " " for sectionName in masterIndex.keys(): print >>out, " <" + sectionName + ">" elName = sectionName[:-1] items = masterIndex[sectionName] for itemName in items.keys(): print >>out, " <" + elName + "" print >>out, " name=\"" + QuoteForAtt(itemName) + "\"" print >>out, " file=\"" + QuoteForAtt(items[itemName]) + "\"/>" print >>out, " " print >>out, " " for databaseName in DatabaseNames: databasePath = os.path.join(TiesRoot, databaseName) ConvertDatabase( TiesRoot, databaseName, databasePath, out, masterIndex, doStoryboards, doPictures, doTargets) print >>out, "" def IndexDatabase(rootPath, databaseName, databasePath, masterIndex, out): storyboardFiles = [] FindFilesOfType(databasePath, '.st0', storyboardFiles) pictureFiles = [] FindFilesOfType(databasePath, '.pn0', pictureFiles) targetFiles = [] FindFilesOfType(databasePath, '.tn0', targetFiles) for storyboardFileName in storyboardFiles: fileName = os.path.join(databasePath, storyboardFileName) storyboardName = fileName[len(rootPath) + 1:] txt = open(fileName, 'r').read() IndexStoryboard(masterIndex, storyboardName, txt, out) for pictureFileName in pictureFiles: fileName = os.path.join(databasePath, pictureFileName) pictureName = fileName[len(rootPath) + 1:] txt = open(fileName, 'r').read() IndexPicture(masterIndex, pictureName, txt, out) for targetFileName in targetFiles: fileName = os.path.join(databasePath, targetFileName) targetName = fileName[len(rootPath) + 1:] txt = open(fileName, 'r').read() IndexTarget(masterIndex, targetName, txt, out) def ConvertDatabase( rootPath, databaseName, databasePath, out, masterIndex, doStoryboards, doPictures, doTargets): storyboardFiles = [] FindFilesOfType(databasePath, '.st0', storyboardFiles) pictureFiles = [] FindFilesOfType(databasePath, '.pn0', pictureFiles) targetFiles = [] FindFilesOfType(databasePath, '.tn0', targetFiles) print >>out, " " if doStoryboards: print >>out, " " for storyboardFileName in storyboardFiles: fileName = os.path.join(databasePath, storyboardFileName) storyboardName = fileName[len(rootPath) + 1:] txt = open(fileName, 'r').read() CompileStoryboard(storyboardName, txt, out, masterIndex) print >>out, " " if doPictures: print >>out, " " for pictureFileName in pictureFiles: fileName = os.path.join(databasePath, pictureFileName) pictureName = fileName[len(rootPath) + 1:] txt = open(fileName, 'r').read() CompilePicture(pictureName, txt, out) print >>out, " " if doTargets: print >>out, " " for targetFileName in targetFiles: fileName = os.path.join(databasePath, targetFileName) targetName = fileName[len(rootPath) + 1:] txt = open(fileName, 'r').read() CompileTarget(targetName, txt, out) print >>out, " " print >>out, " " def IndexStoryboard(masterIndex, storyboardFileName, txt, out): lines = txt.split('\n') storyboardName = lines[1].strip() masterIndex['storyboards'][storyboardName.lower()] = storyboardFileName i = -1 for word in ('.synonyms', '.synonym', '.SYNONYMS', '.SYNONYM'): if word in lines: i = lines.index(word) break if i == -1: return while i < len(lines): line = lines[i].strip().lower() if len(line) > 0: if line in ('.definition', '.description', '.content', '.contents'): break masterIndex['storyboards'][line] = storyboardFileName i += 1 def IndexPicture(masterIndex, pictureFileName, txt, out): pictureName = txt.split('\n')[1].strip().strip('"').lower() masterIndex['pictures'][pictureName] = pictureFileName def IndexTarget(masterIndex, targetFileName, txt, out): targetName = txt.split('\n')[1].strip().strip('"').lower() masterIndex['targets'][targetName] = targetFileName def FindFilesOfType(directoryRoot, suffix, result, subDirectory=''): fileNames = os.listdir(os.path.join(directoryRoot, subDirectory)) for fileName in fileNames: fullFilePath = os.path.join(directoryRoot, subDirectory, fileName) st = os.lstat(fullFilePath) if stat.S_ISDIR(st.st_mode): FindFilesOfType(directoryRoot, suffix, result, os.path.join(subDirectory, fileName)) else: if fileName.lower()[-len(suffix):] == suffix: result.append(os.path.join(subDirectory, fileName)) def QuoteHTMLText(txt): return txt.replace('&', '&').replace('<', '<').replace('>', '>') def CompileMasterIndex(txt, out): print >>out, txt def EmitWarning(interp, txt): debugOut = interp['debugOut'] debugOut.write(QuoteForText(txt) + '\n') def EmitText(interp, txt): out = interp['out'] out.write(QuoteForText(txt) + '\n') def EmitAttributes(interp, attributes): out = interp['out'] for attName in attributes.keys(): out.write(' ' + attName + '="') out.write(QuoteForAtt(attribtes[attName])) out.write('"') def EmitElementBegin(interp, elementName, attributes): out = interp['out'] out.write( '<' + elementName) EmitAttributes(interp, attributes) out.write('>') def EmitElementEnd(interp, elementName): out = interp['out'] out.write( '') def EmitElement(interp, elementName, attributes): out = interp['out'] def ResolveLink(interp, sub, word): word = word.strip().lower() masterIndex = interp['masterIndex'] subIndex = masterIndex[sub] if not subIndex.has_key(word): return None href = subIndex[word] href = HRefRoot + href return href ######################################################################## # HML Storyboard Interpreter Commands def DoSetFont(interp, name, size): interp['font'] = (name, size) def DoButtonFont(interp, name, size): interp['buttonFont'] = (name, size) def DoDefaultFont(interp, name, size): interp['defaultFont'] = (name, size) def DoDefinitionFont(interp, name, size): interp['definitionFont'] = (name, size) def DoControlsFont(interp, name, size): interp['controlsFont'] = (name, size) def DoNl(interp): out = interp['out'] out.write('
\n') def DoSp(interp): out = interp['out'] out.write(' ') def DoLineSpace(interp, space): interp['lineSpace'] = space def DoSpaces(interp, count): out = interp['out'] for i in range(0, int(count)): out.write(' ') def DoPara(interp): out = interp['out'] out.write('

\n') def DoParaIndent(interp, indent): interp['paraIndent'] = indent def DoIndent(interp): out = interp['out'] paraIndent = interp.get('paraIndent', 4) for i in range(0, paraIndent): out.write(' ') def DoTopMargin(interp, margin): interp['topMargin'] = margin def DoBottomMargin(interp, margin): interp['bottomMargin'] = margin def DoLeftMargin(interp, margin): interp['leftMargin'] = margin def DoRightMargin(interp, margin): interp['rightMargin'] = margin def GetMarginStack(interp): if not interp.has_key('marginStack'): interp['marginStack'] = [] return interp['marginStack'] def DoSaveMargins(interp): marginStack = GetMarginStack(interp) marginStack.append(( interp.get('topMargin', 0), interp.get('bottomMargin', 0), interp.get('leftMargin', 0), interp.get('rightMargin', 0), )) def DoRestoreMargins(interp): marginStack = GetMarginStack(interp) if len(marginStack) == 0: return (interp['topMargin'], interp['bottomMargin'], interp['leftMargin'], interp['rightMargin'], ) = marginStack.pop() def GetSynonyms(interp): if not interp.has_key('synonyms'): interp['synonyms'] = [] return interp['synonyms'] def DoWord(interp, word): consumer = interp['consumer'] if consumer == 'title': interp['title'] = word interp['consumer'] = 'none' elif consumer == 'synonyms': word = word.strip() if len(word) > 0: synonyms = GetSynonyms(interp) synonyms.append(word) elif consumer in ('definition', 'content'): EmitText(interp, word) if consumer in ('none', 'ignore'): pass def DoHome(interp): out = interp['out'] out.write('


\n') def DoPage(interp): out = interp['out'] out.write('
\n') def DoQuote(interp, word): EmitText(interp, word) def DoQuoteLine(interp, line): DoQuote(interp, line) def DoLine(interp, line): DoQuote(interp, line) out = interp['out'] DoNl(interp) def DoLines(interp, lines): lines = int(lines) if lines <= 0: return for i in range(0, lines): DoNl(interp) def DoLeft(interp, line): out = interp['out'] out.write('
') DoQuote(interp, line) out.write('
\n') def DoCenter(interp, line): out = interp['out'] out.write('
') DoQuote(interp, line) out.write('
\n') def DoRight(interp, line): out = interp['out'] out.write('
') DoQuote(interp, line) out.write('
\n') def DoPageTarget(interp, name, ref): interp['pageTarget'] = (name, ref) def DoDefinitionTarget(interp, name, ref): interp['definitionTarget'] = (name, ref) def DoControlsTarget(interp, name, ref): interp['controlsTarget'] = (name, ref) def DoFullEntryTarget(interp, name, ref): interp['fullEntryTarget'] = (name, ref) def DoDefaultButtonName(interp, name): interp['defaultButtonName'] = name def DoRect(interp, width, height): DoQuote(interp, '[Rect width ' + width + ' height ' + height + ']\n') def DoMoveto(interp, x, y): DoQuote(interp, '[MoveTo x ' + x + ' y ' + y + ']\n') def DoRmoveto(interp, x, y): DoQuote(interp, '[RMoveTo x ' + x + ' y ' + y + ']\n') def GetTabs(interp): if not interp.has_key('tabs'): interp['tabs'] = {} return interp['tabs'] def DoDefTab(interp, tabnum, pos): tabs = GetTabs(interp) tabs[int(tabnum)] = int(pos) def DoTab(interp, tabnum): tabs = GetTabs(interp) tab = 0 tabnum = int(tabnum) if tabs.has_key(tabnum): tab = tabs[tabnum] if tab == 0: return out = interp['out'] for i in range(0, tab): out.write(' ') def DoOverstrike(interp): pass def DoButton(interp, word): DoTilde(interp, word) def DoButtonline(interp, word): DoTilde(interp, word) def DoButton1(interp, word): DoTilde(interp, word) def DoTilde(interp, word): out = interp['out'] href = ResolveLink(interp, 'storyboards', word) if href == None: EmitWarning(interp, "WARNING: Undefined storyboard link: '" + word + "'") href = '#' out.write('' + QuoteForText(word) + '\n') def DoPicture(interp, picture): out = interp['out'] href = ResolveLink(interp, 'pictures', picture) if href == None: EmitWarning(interp, "WARNING: Undefined picture link: '" + picture + "'") href = '#' out.write('' + QuoteForAtt(picture) + '\n') def DoTarget(interp, name, ref): out = interp['out'] hrefTarget = ResolveLink(interp, 'targets', name) if hrefTarget == None: EmitWarning(interp, "WARNING: Undefined target name link: '" + name + "'") hrefTarget = '#' hrefStoryboard = ResolveLink(interp, 'storyboards', ref) if hrefStoryboard == None: EmitWarning(interp, "WARNING: Undefined target ref link: '" + ref + "'") hrefStoryboard = '#' out.write('' + QuoteForText(name) + '\n') def DoRef(interp, word): DoTilde(interp, word) def DoRem(interp, remark): out = interp['out'] out.write("\n") def DoFreeStamps(interp): pass def DoForth(interp): pass def DoProcess(interp): pass def DoF(interp): pass def DoInit(interp): pass def DoPilePos(interp, x, y): interp['pilePos'] = (x, y) def DoPileSize(interp, width, height): interp['pileSize'] = (width, height) def DoNewPile(interp, pileclass): pass def DoZap(interp): pass def DoEnd(interp): pass def DoTitle(interp): EmitWarning(interp, "Found title.") interp['mode'] = 'line' interp['consumer'] = 'title' interp['out'] = interp['debugOut'] def DoDo(interp, name): pass def DoStartDefinition(interp): pass def DoEndDefinition(interp): pass def DoDefine(interp, name): pass def DoStartArticle(interp): pass def DoEndArticle(interp): pass def DoDoArticulate(interp, name): pass def DoEnergize(interp): pass def DoArticulate(interp, name): pass def DoIndex(interp, name): pass def DoReturn(interp): pass def DoGo(interp, name): pass def DoSynonyms(interp): EmitWarning(interp, "Found synonyms.") interp['mode'] = 'line' interp['consumer'] = 'synonyms' interp['out'] = interp['debugOut'] def DoSynonym(interp): DoSynonyms(interp) def DoDefinition(interp): EmitWarning(interp, "Found definition.") interp['mode'] = 'word' interp['consumer'] = 'definition' interp['out'] = interp['definitionOut'] def DoDescription(interp): #print ("Section: DESCRIPTION") DoDefinition(interp) def DoContents(interp): DoContent(interp) def DoContent(interp): EmitWarning(interp, "Found content.") interp['mode'] = 'word' interp['consumer'] = 'content' interp['out'] = interp['contentOut'] def DoControls(interp): DoContent(interp) def DoNotes(interp): EmitWarning(interp, "Found notes.") interp['mode'] = 'word' interp['consumer'] = 'ignore' interp['out'] = interp['debugOut'] def DoNote(interp): DoNotes(interp) def DoBye(interp): pass def DoSetupControlsPile(interp): pass def DoSetupDefinitionPile(interp): pass def DoSetupContentsPile(interp): pass def DoPile(interp, index): pass def DoNamePile(interp, name): pass def DoUseLinkedPile(interp, name): pass def DoUseParentPile(interp): pass def DoLocal(interp, name): pass def DoGlobal(interp, name): pass def DoCompileArticle(interp, name): pass def DoCompileDefinition(interp, name): pass def DoCompileControls(interp, name): pass ######################################################################## # HML Storyboard Interpreter Command Metadata StoryboardCommands = { '.font': { 'func': DoSetFont, 'args': ['name', 'size'], }, '.button-font': { 'func': DoButtonFont, 'args': ['name', 'size'], 'func': DoButtonFont, }, '.default-font': { 'func': DoDefaultFont, 'args': ['name', 'size'], }, '.definition-font': { 'func': DoDefinitionFont, 'args': ['name', 'size'], }, '.controls-font': { 'func': DoControlsFont, 'args': ['name', 'size'], }, '.nl': { 'func': DoNl, 'args': [], }, '.sp': { 'func': DoSp, 'args': [], }, '.line-space': { 'func': DoLineSpace, 'args': ['space'], }, '.spaces': { 'func': DoSpaces, 'args': ['count'], }, '.para': { 'func': DoPara, 'args': [], }, '.para-indent': { 'func': DoParaIndent, 'args': ['indent'], }, '.indent': { 'func': DoIndent, 'args': [], }, '.top-margin': { 'func': DoTopMargin, 'args': ['margin'], }, '.bottom-margin': { 'func': DoBottomMargin, 'args': ['margin'], }, '.left-margin': { 'func': DoLeftMargin, 'args': ['margin'], }, '.right-margin': { 'func': DoRightMargin, 'args': ['margin'], }, '.save-margins': { 'func': DoSaveMargins, 'args': [], }, '.restore-margins': { 'func': DoRestoreMargins, 'args': [], }, '.home': { 'func': DoHome, 'args': [], }, '.page': { 'func': DoPage, 'args': [], }, '.quote': { 'func': DoQuote, 'args': ['word'], }, '.quote-line': { 'func': DoQuoteLine, 'args': ['line'], 'lines': 1, }, '.line': { 'func': DoLine, 'args': ['line'], 'lines': 1, }, '.lines': { 'func': DoLines, 'args': ['lines'], }, '.left': { 'func': DoLeft, 'args': ['line'], 'lines': 1, }, '.center': { 'func': DoCenter, 'args': ['line'], 'lines': 1, }, '.right': { 'func': DoRight, 'args': ['line'], 'lines': 1, }, '.picture': { 'func': DoPicture, 'args': ['picture'], 'lines': 1, }, '.page-target': { 'func': DoPageTarget, 'args': ['name', 'ref'], 'lines': 2, }, '.definition-target': { 'func': DoDefinitionTarget, 'args': ['name', 'ref'], 'lines': 2, }, '.controls-target': { 'func': DoControlsTarget, 'args': ['name', 'ref'], 'lines': 2, }, '.full-entry-target': { 'func': DoFullEntryTarget, 'args': ['name', 'ref'], 'lines': 2, }, '.default-button-name': { 'func': DoDefaultButtonName, 'args': ['name'], 'lines': 1, }, '.rect': { 'func': DoRect, 'args': ['width', 'height'], }, '.moveto': { 'func': DoMoveto, 'args': ['x', 'y'], }, '.rmoveto': { 'func': DoRmoveto, 'args': ['x', 'y'], }, '.def-tab': { 'func': DoDefTab, 'args': ['tabnum', 'pos'], }, '.tab': { 'func': DoTab, 'args': ['tabnum'], }, '.overstrike': { 'func': DoOverstrike, 'args': [], }, '.button': { 'func': DoButton, 'args': ['word'], 'lines': 1, }, '.buttonline': { 'func': DoButtonline, 'args': ['word'], 'lines': 1, }, '.button1': { 'func': DoButton1, 'args': ['word'], 'lines': 1, }, '.~': { 'func': DoTilde, 'args': ['word'], 'tilde': 1, }, '.ref': { 'func': DoRef, 'args': ['word'], }, '.rem': { 'func': DoRem, 'args': ['remark'], 'lines': 1, }, '.free-stamps': { 'func': DoFreeStamps, 'args': [], }, '.target': { 'func': DoTarget, 'args': ['name', 'ref'], 'lines': 2, }, '.forth': { 'func': DoForth, 'args': [], }, '.process': { 'func': DoProcess, 'args': [], }, '.f': { 'func': DoF, 'args': [], }, '.init': { 'func': DoInit, 'args': [], }, '.pile-pos': { 'func': DoPilePos, 'args': ['x', 'y'], }, '.pile-size': { 'func': DoPileSize, 'args': ['width', 'height'], }, '.new-pile': { 'func': DoNewPile, 'args': ['class'], }, '.zap': { 'func': DoZap, 'args': [], }, '.end': { 'func': DoEnd, 'args': [], }, '.title': { 'func': DoTitle, 'args': [], }, '.do': { 'func': DoDo, 'args': ['name'], 'lines': 1, }, '.start-definition': { 'func': DoStartDefinition, 'args': [], 'lines': 1, }, '.end-definition': { 'func': DoEndDefinition, 'args': [], 'lines': 1, }, '.define': { 'func': DoDefine, 'args': ['name'], 'lines': 1, }, '.start-article': { 'func': DoStartArticle, 'args': [], 'lines': 1, }, '.end-article': { 'func': DoEndArticle, 'args': [], 'lines': 1, }, '.do-articulate': { 'func': DoDoArticulate, 'args': ['name'], 'lines': 1, }, '.energize': { 'func': DoEnergize, 'args': [], 'lines': 1, }, '.articulate': { 'func': DoArticulate, 'args': ['name'], 'lines': 1, }, '.index': { 'func': DoIndex, 'args': ['name'], 'lines': 1, }, '.return': { 'func': DoReturn, 'args': [], }, '.go': { 'func': DoGo, 'args': ['name'], 'lines': 1, }, '.synonyms': { 'func': DoSynonyms, 'args': [], 'section': 'synonyms', }, '.synonym': { 'func': DoSynonym, 'args': [], 'section': 'synonyms', }, '.definition': { 'func': DoDefinition, 'args': [], 'section': 'definition', }, '.description': { 'func': DoDescription, 'args': [], 'section': 'definition', }, '.contents': { 'func': DoContents, 'args': [], 'section': 'contents', }, '.content': { 'func': DoContent, 'args': [], 'section': 'contents', }, '.controls': { 'func': DoControls, 'args': [], }, '.notes': { 'func': DoNotes, 'args': [], 'section': 'notes', }, '.note': { 'func': DoNote, 'args': [], 'section': 'notes', }, '.bye': { 'func': DoBye, 'args': [], }, '.setup-controls-pile': { 'func': DoSetupControlsPile, 'args': [], }, '.setup-definition-pile': { 'func': DoSetupDefinitionPile, 'args': [], }, '.setup-contents-pile': { 'func': DoSetupContentsPile, 'args': [], }, '.pile': { 'func': DoPile, 'args': ['index'], }, '.name-pile': { 'func': DoNamePile, 'args': ['name'], }, '.use-linked-pile': { 'func': DoUseLinkedPile, 'args': ['name'], }, '.use-parent-pile': { 'func': DoUseParentPile, 'args': [], }, '.local': { 'func': DoLocal, 'args': ['name'], }, '.global': { 'func': DoGlobal, 'args': ['name'], }, '.compile-article': { 'func': DoCompileArticle, 'args': ['name'], }, '.compile-definition': { 'func': DoCompileDefinition, 'args': ['name'], }, '.compile-controls': { 'func': DoCompileControls, 'args': ['name'], }, } WhiteSpace = ' \t\n\r' NewLine = '\n\r' def IsDone(interp): return interp['index'] >= len(interp['buf']) def GetChar(interp): if IsDone(interp): return None #print ("GetChar", interp['index'], interp['buf'][interp['index']]) return interp['buf'][interp['index']] def NextChar(interp): interp['index'] += 1 def SkipTo(interp, chars): while 1: ch = GetChar(interp) if (ch == None) or (ch in chars): return NextChar(interp) def SkipChars(interp, chars): while 1: ch = GetChar(interp) if (ch == None) or (ch not in chars): return NextChar(interp) def SkipWhite(interp): SkipChars(interp, WhiteSpace) def SkipToWhite(interp): SkipTo(interp, WhiteSpace) def ReadTo(interp, delim): start = interp['index'] SkipTo(interp, delim) end = interp['index'] NextChar(interp) #print (start, end) word = interp['buf'][start:end] return word def ReadWord(interp): SkipWhite(interp) word = ReadTo(interp, WhiteSpace) #print ("WORD", word) return word def ReadLine(interp): line = ReadTo(interp, NewLine) #print ("LINE", line) return line def Interpret(interp, out): debugOut = interp['debugOut'] while not IsDone(interp): mode = interp['mode'] consumer = interp['consumer'] if mode == 'word': word = ReadWord(interp) elif mode == 'line': word = ReadLine(interp) else: EmitWarning(interp, "WARNING: Invalid interp mode: " + mode) break #print ("CONSUMER", consumer, "MODE", mode, "WORD", word) if consumer == 'ignore': continue wordLower = word.lower() if ((wordLower not in ( '.title', '.synonym', '.synonyms', '.definition', '.description', '.contents', '.content', '.controls', '.notes', )) and ((consumer not in ( 'definition', 'content', 'none', )) or (not StoryboardCommands.has_key(wordLower)))): if (len(word) > 2) and word[0:2] == '.~': EmitWarning(interp, "WARNING: Bad link syntax: " + word) DoWord(interp, word) continue cmd = StoryboardCommands[wordLower] args = cmd.get('args', []) lines = cmd.get('lines', 0) tilde = cmd.get('tilde', 0) section = cmd.get('section', None) argValues = [interp,] start = interp['index'] if tilde: argValues.append(ReadTo(interp, '~')) elif lines: for line in range(0, lines): argValues.append(ReadLine(interp)) else: for argName in args: argValues.append(ReadWord(interp)) if len(args) != (len(argValues) - 1): EmitWarning( interp, "WARNING: Mismatched args: word '" + word + "' args " + repr(args) + " argValues " + repr(argValues)) break argDict = {} for i in range(0, len(args)): argDict[args[i]] = argValues[i] #print (word, argDict) func = cmd['func'] result = apply(func, argValues) title = interp.get('title', '') synonyms = interp.get('synonyms', []) print >>out, " " print >>out, " " + QuoteForText(title) print >>out, " " print >>out, " " for synonym in synonyms: print >>out, " " print >>out, " " + QuoteForText(synonym) print >>out, " " print >>out, " " print >>out, " " print >>out, interp['definitionOut'].getvalue() print >>out, " " print >>out, " " print >>out, interp['contentOut'].getvalue() print >>out, " " debugOutput = interp['debugOut'].getvalue() if len(debugOutput) > 0: print >>out, " " print >>out, "" print >>out, " " def CompileStoryboard(storyboardName, txt, out, masterIndex): print >>out, " " #print >>out, QuoteHTMLText(txt) debugOut = StringIO.StringIO() definitionOut = StringIO.StringIO() contentOut = StringIO.StringIO() interp = { 'buf': txt, 'index': 0, 'definitionOut': definitionOut, 'contentOut': contentOut, 'debugOut': debugOut, 'out': debugOut, 'masterIndex': masterIndex, 'mode': 'word', 'consumer': 'none', } Interpret(interp, out) print >>out, " " ######################################################################## # Object Compiler PictureClasses = { 'Stamp': { 'name': 'Stamp', }, 'Raster': { 'name': 'Raster', }, 'Picture': { 'name': 'Picture', }, 'ScaledRaster': { 'name': 'ScaledRaster', }, 'UnitPicture': { 'name': 'UnitPicture', }, } TargetClasses = { 'PageTrackerTarget': { 'name': 'PageTrackerTarget', }, 'MenuTarget': { 'name': 'MenuTarget', }, 'Target': { 'name': 'Target', }, 'SliderTarget': { 'name': 'SliderTarget', }, 'AnimatedTarget': { 'name': 'AnimatedTarget', }, 'TextCanvasTarget': { 'name': 'TextCanvasTarget', }, 'TextEditTarget': { 'name': 'TextEditTarget', }, 'LinkTarget': { 'name': 'LinkTarget', }, 'PopupTarget': { 'name': 'PopupTarget', }, 'ScrollbarTarget': { 'name': 'ScrollbarTarget', }, } def CompilePicture(picturePath, txt, out): lines = txt.split('\n') pictureClass = lines[0] pictureName = lines[1].strip().strip('"') print >>out, " >out, " path=\"" + picturePath + "\"" print >>out, " class=\"" + pictureClass + "\"" print >>out, " name=\"" + pictureName + "\"" # Stamp Picture ScaledRaster UnitPicture Raster pictureWidth = 1 pictureHeight = 1 elements = '' if pictureClass in ('Stamp', 'Picture', 'ScaledRaster', 'UnitPicture'): (pictureWidth, pictureHeight) = [ int(val) for val in txt.strip().split('\n')[-1].strip().split(' ')] if pictureClass in ('Raster', 'ScaledRaster'): rasterFileName = txt.split('\n')[2][1:].split(')')[0] if rasterFileName[0] == '%': rasterFileName = rasterFileName.split('/')[-1] if rasterFileName[0] == '!': rasterFileName = rasterFileName[1:] rasterFileName += '.png' else: rasterFileName = picturePath.split('/')[-1] + '.png' rasterPath = "/".join(picturePath.split('/')[:-1] + [rasterFileName]) info = os.popen("file '" + rasterPath + "'").read().strip() if pictureClass == 'Raster': (pictureWidth, pictureHeight) = [ int(val) for val in info.split(',')[1].strip().split(' x ')] print >>out, " rasterpath=\"" + rasterPath + "\"" print >>out, " rasterinfo=\"" + info + "\"" print >>out, " width=\"" + str(pictureWidth) + "\"" print >>out, " height=\"" + str(pictureHeight) + "\"" if elements: print >>out, " >" print elements print >>out, " >out, " >>out, txt print >>out, " ]]>" ItemPathCommandArgs = { 'moveto': ('x', 'y'), 'lineto': ('x', 'y'), 'closepath': (), 'rr': (), 'rrp': ('x', 'y', 'w', 'h'), 'arc': ('x', 'y', 'r', 'a1', 'a2'), 'rectpath': ('x', 'y', 'w', 'h'), 'rrectpath': ('x', 'y', 'w', 'h', 'r'), } class GState: def __init__(self): self.graphics = [] self.path = [] self.unitscale = False def setUnitScale(self, val): self.unitScale = val def addPath(self, str): self.path.append(str) def flushPath(self): if self.path: self.graphics.append( '') self.path = [] def addGraphics(self, str): self.flushPath() self.graphics.append(str) def getSVG(self): self.flushPath() return ( '\n' + '\n'.join(self.graphics) + '\n' + '\n' ) ItemPathCommandFuncs = { 'moveto': ( lambda gstate, x, y: gstate.addPath('M ' + str(float(x) * 1000) + ' ' + str(float(y) * 1000))), 'lineto': ( lambda gstate, x, y: gstate.addPath(str(float(x) * 1000) + ' ' + str(float(y) * 1000))), 'closepath': ( lambda gstate: gstate.addPath('z')), 'rr': ( lambda gstate: (gstate.setUnitScale(True), gstate.addGraphics( ''))), 'rrp': ( lambda gstate, x, y, w, h: (gstate.setUnitScale(True), gstate.addGraphics( ''))), 'arc': ( lambda gstate, x, y, r, a1, a2: # TODO, just a dot for now. gstate.addPath( 'M ' + str(float(x) * 1000) + ' ' + str(float(y) * 1000))), 'rectpath': ( lambda gstate, x, y, w, h: gstate.addGraphics( '')), 'rrectpath': ( lambda gstate, x, y, w, h, r: gstate.addGraphics( '')), } def CompileTarget(targetPath, txt, out): lines = txt.split('\n') targetClass = lines[0] targetName = lines[1].strip().strip('"') print >>out, " >out, " path=\"" + targetPath + "\"" print >>out, " class=\"" + targetClass + "\"" print >>out, " name=\"" + targetName + "\"" # LinkTarget (itempath) # many # PopupTarget (itempath) # many # Target # command-exec quietpicture wrong-button psh-button fork-button # full-entry-background full-entry-button topics-button home-button # quit-button refresh-button return-button tracer show-button # show-background other-button index-button # AnimatedTarget # spin blink-close blink-open chomp pivot spin # PageTrackerTarget # back-button next-button first-button last-button # SliderTarget # slider # MenuTarget # compass-menu confirm-menu days-menu font-menu hours-menu # scroll-menu test-menu tree-menu zoom-menu free-button # ScrollbarTarget # fbgray-scroller file-view-scroller fixed-scroller hscroller scroller text-scroller # TextEditTarget # command-text filename unlabeled-text # TextCanvasTarget # file-view jabberwocky-text-canvas text-canvas forth-text elements = '' # Detect and compile the ItemPath itemPath = '' target = '/ItemPath {' start = txt.find(target) if start != -1: start += len(target) target = '} def' finish = txt.find(target, start) if finish != -1: itemPath = txt[start:finish].strip() # Detect and strip "{ ... } unit-scale" unitScale = 'false' target = '{' start = itemPath.find(target) if start != -1: start += len(target) target = '} unit-scale' finish = itemPath.find(target, start) if finish != -1: unitScale = 'true' itemPath = itemPath[start:finish].strip() if 0: # Compile itemPath into xml elements += ' \n' lines = itemPath.strip().split('\n') for line in lines: tokens = [token.strip() for token in line.strip().split(' ') if token] args = tokens[0:-1] operator = tokens[-1] if not ItemPathCommandArgs.has_key(operator): sys.stderr.write('Unknown operator: ' + line + '\n') continue argNames = ItemPathCommandArgs[operator] if len(argNames) != len(args): sys.stderr.write('Wrong number of args: ' + line + '\n') continue elements += ' <' + operator for i in range(0, len(args)): arg = args[i] argName = argNames[i] elements += ' ' + argName + '="' + arg + '"' elements += '/>\n' elements += ' \n' else: gstate = GState() lines = itemPath.strip().split('\n') for line in lines: tokens = [token.strip() for token in line.strip().split(' ') if token] args = tokens[0:-1] operator = tokens[-1] if not ItemPathCommandFuncs.has_key(operator): sys.stderr.write('Unknown operator: ' + line + '\n') continue fn = ItemPathCommandFuncs[operator] apply(fn, [gstate] + args) svg = gstate.getSVG() f = open(targetPath + '.svg', "w") f.write(svg) f.close() elements += ' \n' elements += svg elements += ' \n' sys.stderr.write(elements + '\n\n') # Handle PopupTarget if targetClass == 'PopupTarget': dx = 0 dy = 0 scale = 1 rasterFileName = '' target = '{' start = txt.find(target) if start != -1: start += len(target) target = '}' finish = txt.find(target, start) if finish != -1: initArgs = txt[start:finish].strip() if initArgs: #sys.stderr.write(targetClass + '\n' + targetName + '\n' + initArgs + '\n\n\n') #sys.stderr.flush() lines = initArgs.split('\n') (dx, dy, scale) = [ val for val in lines[0].strip().split(' ')] rasterFileName = lines[1].strip()[1:].split(')')[0] if rasterFileName[0] == '%': rasterFileName = rasterFileName.split('/')[-1] rasterFileName += '.png' rasterPath = "/".join(targetPath.split('/')[:-1] + [rasterFileName]) info = os.popen("file '" + rasterPath + "'").read().strip() (rasterWidth, rasterHeight) = [ int(val) for val in info.split(',')[1].strip().split(' x ')] print >>out, " dx=\"" + str(dx) + "\"" print >>out, " dy=\"" + str(dy) + "\"" print >>out, " scale=\"" + str(scale) + "\"" print >>out, " rasterpath=\"" + rasterPath + "\"" print >>out, " rasterinfo=\"" + info + "\"" print >>out, " rasterwidth=\"" + str(rasterWidth) + "\"" print >>out, " rasterheight=\"" + str(rasterHeight) + "\"" if elements: print >>out, " >" print elements print >>out, " >out, " >>out, txt print >>out, " ]]>" ######################################################################## ConvertDatabases(sys.stdout) ########################################################################