Sublime Forum

PlistJsonConverter

#1

I always hear people asking about how to convert a plist (tmLanguage, color scheme, etc), so I created a generic plugin to simply toggle your view between plist to json. It assumes that the files of either type are “UTF8”, so keep that in mind. It also just operates on the view buffer, so you can toggle back and forth, and then save the file name as whatever you want.

If you like to work in JSON, that is fine. I even allow you to use C style comments in your JSON (they will be removed on conversion back to plist, and vice versa for HTML/XML comments in your plist XML). I also try to catch trailing JSON commas on conversion (those always get me :smile: ) to plist as well.

Conversion of PHP tmLanguage as an example


Repository: github.com/facelessuser/PlistJsonConverter

0 Likes

SublimeJSONColorSchemeEditor - GUI edit syntax colors
SublimeJSONColorSchemeEditor - GUI edit syntax colors
#2

Excuse my naivetĂ©, but how do I install these files? I’m on build 2181 and I managed to find the Zip download button :smile:

In particular, where do I place the ‘Default.sublime-settings’?

0 Likes

#3

Just put it all in a folder in the Packages directory.

0 Likes

#4

Make sure that when you copy it to the package folder, the folder is named “PlistJsonConverter”. Github will name the folder in the zip something dumb like “facelessuser-PlistJsonConverter-123456” etc.

0 Likes

#5

Hi. Thank you. I managed to install it :wink:. I placed the settings and ‘Default.sublime-commands’ in my existing files, as i wasn’t keen to have two files with the same name.

My ‘Python.tmLanguage’ dropped from 3 to 2000 lines! (Still many more than the JS version though.)

It displays a load of redundant \n\t\t - this is good as it’s best to delete them.

I would prefer that it create a new tab though :smile:

0 Likes

#6

That is just how plugins add commands to the command palette. You can override them in your Packages/User folder, but I would leave it in the plugin folder. As soon as I add this to Package Control. It is always going to be updated there.

[quote=“agibsonsw”]
I would prefer that it create a new tab though :smile:[/quote]

Yeah, I can add an option for opening it in a new buffer. That is no biggie.

0 Likes

#7

I too was unsatisfied with AAAPackageDev and, following your ScopeHunter, this plugin couldn’t have come at a better time. Thanks for putting together such wonderful tools.

That said, the workflow is not ideal, although it may simpler for folks who aren’t (as I am) learning regular expressions at the same time.

(The fastest process I found for what I was doing earlier was editing the JSON file, copying & pasting the contents into the .tmLanguage file, converting via the palette, saving, and then flipping back to my test document to see the horrors I had wrought.)

Would it be possible to use sublime’s build system with this plugin?

0 Likes

#8

[quote=“quodlibet”]I too was unsatisfied with AAAPackageDev and, following your ScopeHunter, this plugin couldn’t have come at a better time. Thanks for putting together such wonderful tools.

That said, the workflow is not ideal, although it may simpler for folks who aren’t (as I am) learning regular expressions at the same time.

(The fastest process I found for what I was doing earlier was editing the JSON file, copying & pasting the contents into the .tmLanguage file, converting via the palette, saving, and then flipping back to my test document to see the horrors I had wrought.)

Would it be possible to use sublime’s build system with this plugin?[/quote]

Is it that you just want it to convert, create, and save the plist or json file, opposed to just converting? I just need to understand the specifics before I can do any implementation.

0 Likes

#9

I’m not sure :confused:

I find it simpler (thus far) to write syntax files in JSON rather than plist. So I think what I am looking for is to convert “XXX.tmLanguage.JSON” to “XXX.tmLanguage” with the minimum of fuss.

There may be an external tool I can use for this that I am not aware of. I’ve had trouble with AAAPackageDev, but it may be better to work with guilermooo to resolve the issues there. On the other hand, if it seems a worthwhile addition to what you are trying to build, what I’m looking is for Ctrl+B to convert.

I hope some of the above makes sense :smile:

0 Likes

#10

This is why I am asking. If it is just you want to convert XXX.tmLanguage.JSON to XXX.tmLanguage; I don’t think a build system is really needed, I can easily add it the plugin, and you can bind it to whatever key combo you want or just access it from the command palette.

Either a command or build system can be implemented though. I don’t mind adding more in depth features, or even build systems. I currently don’t plan on using AAAPackageDev, so having some standalone plugins to do commonly desired things is what I am aiming for; that way you can pick and choose what you want, and each one can be even more specialized in what they do, because that is all they will do. This plugin is a bit more flexible because it can do any plist you like: tmTheme, tmLanguages, tmPreference, etc.

I personally like the toggle, because I can just toggle it to json, make changes, and toggle back and save. But if you want to keep the json like a source code file, and the tmLanguage like the compiled code, that can be done by a simple command (no build system required). If you want to search an entire project, then maybe a build system would be helpful, but I still don’t think it would be necessary. Build systems are better for feeding an external executable that does some compile stuff with project variables.

I will play around with some ideas, and see what I can cook up, maybe even a build system :smile:.

0 Likes

#11

Ah. This had not even crossed my mind. I toyed around with this workflow for a few minutes and it seems tenable, but I think that losing the comments might be a deal breaker here. As I said, I am learning regexp amid all this, to say nothing of the intricacies of .tmLanguage files.

Edit:

I generally use build systems on a per-file level. Not being a programmer myself, I forget what you guys actually use them for :smile:

0 Likes

#12

I will add something in to save to a new file.

0 Likes

#13

I should have an acceptable solution for automatically creating the converted file or just creating the conversion in a new buffer. I need to do a little testing tomorrow first before I push it out.

You should be able to define what plist extension will map to which json extension. I will give more info when I push it out.

0 Likes

#14

Version 0.2.0

  • Add commands to save converted file with specified extension
  • For buffer conversion, add setting to open in new buffer
  • Allow the turning off of save commands and/or buffer commands

This should satisfy everyone’s desires hopefully.

I have added commands to save the converted content directly with the extension of your choice. I have added an option to open conversions in their own new view buffer if not using the “save to file” commands. You can turn off the commands you don’t want (“save to file” or “show in buffer”). So now you can set the preference you like, and hide commands you don’t like.

This is flexible enough to be applied to any plist or json extension you want, and you can map the converted info to any extension you want. So this, will work with tmTheme, tmLanguages, tmPreferences, and anything else you want to add.

Settings:

[code]{
“json_language”: “Packages/Javascript/JSON.tmLanguage”,
“plist_language”: “Packages/XML/XML.tmLanguage”,

// When converting buffer open conversion in new buffer
"open_in_new_buffer": true,

// Enable show conversion in a view buffer
"enable_show_in_buffer_commands": true,

// Enable creation of new file based on extension map containing the coverted data
// If the current file to convert does not exist on disk, the converted file will default
// To being shown in a view buffer only, and will not be automatcially saved to disk.
"enable_save_to_file_commands": true,

// When saving converted data to a file, or when opening
// conversion in new buffer use these extension maps for file name.
// Extensions will be evaluated in the order listed below.
// If the file does not match any of the extensions, the current
// extension will be replaced with either "plist" or "json" accordingly.
"conversion_ext": 
    {"plist": "tmLanguage", "json": "tmLanguage.JSON"},
    {"plist": "tmPreferences", "json": "tmPreferences.JSON"},
    {"plist": "tmTheme", "json": "tmTheme.JSON"}
]

}[/code]

Let me know if you find any bugs or have any suggestions. I will make the request to add this to Package Control sometime soon.

0 Likes

#15

Oh, I just wanted to mention, if a build system option is still desired, just let me know; it will take me only a couple of minutes to add. I am really just waiting to see if the new current method is sufficient or not.

0 Likes

#16

The convert & save command is perfect for my purposes.

I’ve encountered an error, which is probably related to my setup. I haven’t worked out what the problem is. I’d be grateful for a pointer.

When trying to convert Markdown.tmLanguage from PLIST to JSON, I get the following in the console:

error: Could not read view buffer as PLIST!
Please see console for more info.
Plist Json Converter Exception:
No module named pyexpat
0 Likes

#17

[quote=“quodlibet”]The convert & save command is perfect for my purposes.

I’ve encountered an error, which is probably related to my setup. I haven’t worked out what the problem is. I’d be grateful for a pointer.

When trying to convert Markdown.tmLanguage from PLIST to JSON, I get the following in the console:

error: Could not read view buffer as PLIST! Please see console for more info. Plist Json Converter Exception: No module named pyexpat [/quote]

I am assuming conversion of any tmLanguage does not work for you. Are you on Linux (the one system I did not test)? Doing a quick search on the forums shows that the Linux version of ST2 does not use Linux’s installed Python, but includes its own Python version (an incomplete version that does not have pyexpat).

If you are using Linux, are you using Ubuntu? I will have to fire this up in Ubuntu and see what I can I do. I think I can possibly just try to include Ubuntu’s pyexpat from its installation. I think they include 2.6 and 2.7 Python. I will get back to you maybe tonight on that if I have time.

I do hope in the future ST2 would include pyexpat like it does on Windows. Hopefully pointed ST2 to a valid 2.6 pyexpat will work, or I will have to come up with some workaround plist conversion on LInux (which will take time).

0 Likes

#18

I actually found the solution not to bad and had a little time to fix it this morning.

Version 0.3.0

  • Fix for Linux pyexpat errors
  • Fix default JSON.tmLanguage path

Please read the readme.md file for more info. The fix by default is for Ubuntu, and you might have to make sure you have a full install of Python2.6 on Linux box instead of the default minimal install Ubuntu provides. I provided a setting to help with adapting this to other distros hopefully as well.

0 Likes

#19

Yes, I’m on Ubuntu. Version 0.3.0 along with the full python 2.6 works perfectly. Thanks for the super quick fix!

Sorry, it took a while to reply.

Alex

0 Likes

#20

[quote=“quodlibet”]Yes, I’m on Ubuntu. Version 0.3.0 along with the full python 2.6 works perfectly. Thanks for the super quick fix!

Sorry, it took a while to reply.

Alex[/quote]

No biggie. As soon as you described the issue, I knew I had seen it mentioned before. Being one of those issues that have already been solved by others, I was pretty confident in the solution. I did fire it up in my Ubuntu VM to make sure though.

0 Likes