Sublime Forum

Correct way to use a complicated pipe in build system

#1

For a small plugin I am developing I’m trying to use a specific build system variant to update a completion file (.json). I already got some help to get the output of the build system command redirected to a file. Now I have to transform the HTML that gets generated by the command to the .json format ST3 understands. I tested this pretty long pipe in the command line and it works:

ikiwiki --setup foo.setup --render "/Users/openmedi/tag.mdwn" | sed 's/<a href="\.\/\(.*\)\/" class="mapitem">.*<\/a>/\1/' | sed '/^\s*$/d' | sed -E '/^<|\s.*/d' | sed '1d' | sed 's/\(.*\)/        \{ "trigger": "\1", "contents": "\1" \},/' | (printf '{\n    "scope": "tag.directive.ikiwiki",\n\n    "completions":\n    [\n' && cat) | sed '$ s/\},/\}\
    ]\
}/'

This does remove the unnecessary parts of the html and transforms the information into the desired format so that I get something that looks like this:

{
   "scope": "tag.directive.ikiwiki",

   "completions":
   [
      { "trigger": "tag1", "contents": "tag1" },
      { "trigger": "tag2", "contents": "tag2" },
      { "trigger": "tag3", "contents": "tag3" },
   ]
}

My build system looks like this:

import sublime, sublime_plugin

class RebuildWikiCommand(sublime_plugin.WindowCommand):
	def run(self, verbose=False, refresh=False, utags=False):
		s = sublime.load_settings("ikiwikiMarkdown.sublime-settings")
		setup_file = s.get("ikiwiki_setup_file")
		working_dir = s.get("ikiwiki_working_dir")
		def build_ikicmd():
			ikicmd = "ikiwiki"
			if setup_file:
				ikicmd+=(" --setup " + setup_file)
			else:
				sublime.status_message("Please add setup file in ikiwikiEditing options!")
			if verbose:
				ikicmd+=(" --verbose")
			if refresh:
				ikicmd+=(" --refresh")
			if utags:
				tag_file = s.get("ikiwiki_tag_file")
				completion_file = s.get("ikiwiki_completion_file")
				sedstuff = """| sed 's/<a href="\.\/\(.*\)\/" class="mapitem">.*<\/a>/\1/'"""
				sedstuff+= """ | sed '/^\s*$/d'"""
				sedstuff+= """ | sed -E '/^<|\s.*/d'"""
				sedstuff+= """ | sed '1d'"""
				sedstuff+= """ | sed 's/\(.*\)/        \{ "trigger": "\1", "contents": "\1" \},/'"""
				sedstuff+= """ | (printf '{\n    "scope": "tag.directive.ikiwiki",\n\n    "completions":\n    [\n' && cat)"""
				sedstuff+= """ | sed '$ s/\},/\}\\n    ]\\n}/'"""
				ikicmd+=(" --render " + tag_file + sedstuff + ' | tee "{}"'.format(completion_file))
   			return ikicmd
		self.window.run_command('exec', {'shell_cmd': build_ikicmd(), 'working_dir': working_dir} )

Sadly it doesn’t work that way. I get the following error:

These weird “SOH” symbols are control characters and indicating “First Character of a Message Header”, which in my case probably means that the pipe is not perfectly preserved in my python code.

I’m really not sure what to do. So any help in making the pipe work or get the conversion of html to ST3-readable completion file .json syntax right is very much appreciated.

0 Likes

#2

At this point you should really start editing the output in Python itself rather than trying to do everything in just one commandline.

Again, see this post for the outline and helpful references.

1 Like