Commits

David Bennett  committed 0bbb37c

Added option to embed in existing jwplayer.js.

  • Participants
  • Parent commits 48c2ad7

Comments (0)

Files changed (1)

File jwskin2js.py

 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 #
-import argparse, os, sys
+import argparse, os, re, sys
 from xml.dom.minidom import parseString
 from zipfile import ZipFile
 
  */
 (function(jwplayer) {{
     jwplayer.html5.defaultSkin = function() {{
-        this.text = '{xml}';
+        this.text = '{text}';
         this.xml = null;
 
         //http://www.w3schools.com/Dom/dom_parser.asp
 }})(jwplayer);
 """
 
-def process(zipfile, jsfile):
+def process(zipfile, jsfile, embed=None):
     """
     Process a zip file and output a js file. Both arguments should be
     file-like objects.
         for element in component.getElementsByTagName('element'):
             element_src = element.getAttribute('src')
             element.setAttribute('src', images[component_name][element_src])
-    jsfile.write(JS_TEMPLATE.format(
-        name=skin_name,
-        xml=''.join(l.strip() for l in skin_xml.toxml().splitlines())))
+    text = ''.join(l.strip() for l in skin_xml.toxml().splitlines())
+    if embed:
+        text, subs = re.subn(r'<\?xml .*?</skin>', text, embed.read())
+        if subs < 1:
+            sys.stderr.write(
+                "Couldn't find any xml files to replace in the embed file.")
+            exit(1)
+        elif subs > 1:
+            sys.stderr.write(
+                "Found too many xml files to replace in the embed file.")
+            exit(2)
+        jsfile.write(text)
+    else:
+        jsfile.write(JS_TEMPLATE.format(name=skin_name, text=text))
+
+def main():
+    parser = argparse.ArgumentParser(
+        description='Compile a JW Player 5 skin to JavaScript.')
+    parser.add_argument(
+        'zipfile', type=argparse.FileType(), help='source skin zip file')
+    parser.add_argument(
+        '-e', '--embed', type=argparse.FileType(), metavar='jwplayer',
+        help='optionally embed into existing jwplayer.js (default is to '
+        'create a standalone JavaScript file)')
+    parser.add_argument(
+        '-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
+        metavar='jsfile', dest='jsfile', help='optionally save to a file '
+        '(default is to write to STDOUT)')
+    args = parser.parse_args()
+    process(args.zipfile, args.jsfile, args.embed)
+
 
 if __name__ == '__main__':
-    parser = argparse.ArgumentParser(
-        description='Compile a JW Player 5 skin to JavaScript.')
-    parser.add_argument('zipfile', type=argparse.FileType())
-    parser.add_argument(
-        '-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
-        metavar='jsfile', dest='jsfile')
-    args = parser.parse_args()
-    process(args.zipfile, args.jsfile)
+    main()