Project

Profile

Help

Revision c89c6b5b

Added by Vincent Le Goff over 4 years ago

Add the feature to write SharpScript

View differences:

src/sharp/engine.py
242 242
            i += 1
243 243

  
244 244
        return text.replace(";;", ";")
245

  
246
    def format(self, content):
247
        """Write SharpScript and return a string.
248

  
249
        This method takes as argument the SharpScript content and formats it.  It therefore replaces the default formatting.  Arguments are escaped this way:
250

  
251
        * If the argument contains space, escape it with braces.
252
        * If the argument contains new line, indent it.
253
        * If the argument contains semi colons, keep it on one line.
254

  
255
        """
256
        instructions = self.split_statements(content)
257

  
258
        # At this stage, the instructions are formatted in Python
259
        lines = []
260
        for arguments in instructions:
261
            function = arguments[0].lower()
262
            arguments = list(arguments[1:])
263

  
264
            # Escape the arguments if necessary
265
            for i, argument in enumerate(arguments):
266
                arguments[i] = self.escape_argument(argument)
267

  
268
            line = function + " " + " ".join(arguments)
269
            lines.append(line.rstrip(" "))
270

  
271
        return "\n".join(lines)
272

  
273
    @staticmethod
274
    def escape_argument(argument):
275
        """Escape the argument if needed."""
276
        if argument.startswith("{"):
277
            pass
278
        elif "\n" in argument:
279
            lines = argument.splitlines()
280
            argument = "{" + "\n    ".join(lines) + "\n}"
281
        elif " " in argument:
282
            argument = "{" + argument + "}"
283

  
284
        return argument

Also available in: Unified diff