# @author 		Avtandil Kikabidze
# @copyright 		Copyright (c) 2008-2014, Avtandil Kikabidze aka LONGMAN (akalongman@gmail.com)
# @link 			http://long.ge
# @license 		GNU General Public License version 2 or later;

import os
import sys
import re
import sublime
import subprocess

import htmlbeautifier

class HtmlFormatter:
	def __init__(self, formatter):
		self.formatter = formatter


	def format(self, text):
		text = text.decode("utf-8")
		opts = self.formatter.settings.get('codeformatter_html_options')


		stderr = ""
		stdout = ""
		options = htmlbeautifier.default_options()

		if ("indent_size" in opts and opts["indent_size"]):
			options.indent_size = opts["indent_size"]
		else:
			options.indent_size = 4


		if ("indent_char" in opts and opts["indent_char"]):
			options.indent_char = str(opts["indent_char"])
		else:
			options.indent_char = "	"

		if ("indent_with_tabs" in opts and opts["indent_with_tabs"]):
			options.indent_with_tabs = True
		else:
			options.indent_with_tabs = False

		if ("preserve_newlines" in opts and opts["preserve_newlines"]):
			options.preserve_newlines = True
		else:
			options.preserve_newlines = False

		if ("max_preserve_newlines" in opts and opts["max_preserve_newlines"]):
			options.max_preserve_newlines = opts["max_preserve_newlines"]
		else:
			options.max_preserve_newlines = 10

		if ("indent_tags" in opts and opts["indent_tags"]):
			options.indent_tags = str(opts["indent_tags"])

		try:
 		 	stdout = htmlbeautifier.beautify(text, options)
		except Exception as e:
		 	stderr = str(e)

		if (not stderr and not stdout):
			stderr = "Formatting error!"

		return stdout, stderr


@@ -376,17 +376,16 @@
 matter%0A%0A
-%0A
 %09def for
@@ -499,17 +499,16 @@
 ions')%0A%0A
-%0A
 %09%09stderr
@@ -574,25 +574,24 @@
 ons()%0A%0A%09%09if 
-(
 %22indent_size
@@ -603,33 +603,8 @@
 opts
- and opts%5B%22indent_size%22%5D)
 :%0A%09%09
@@ -650,51 +650,14 @@
 e%22%5D%0A
-%09%09else:%0A%09%09%09options.indent_size = 4%0A%0A
 %0A%09%09if 
-(
 %22ind
@@ -677,33 +677,8 @@
 opts
- and opts%5B%22indent_char%22%5D)
 :%0A%09%09
@@ -725,22 +725,55 @@
 char%22%5D)%0A
+%0A
 %09%09
-else
+if %22minimum_attribute_count%22 in opts
 :%0A%09%09%09opt
@@ -781,33 +781,72 @@
 ons.
+m
 in
-dent_char = %22%09%22
+imum_attribute_count = opts%5B%22minimum_attribute_count%22%5D
 %0A%0A%09%09if 
-(
 %22ind
@@ -871,38 +871,8 @@
 opts
- and opts%5B%22indent_with_tabs%22%5D)
 :%0A%09%09
@@ -903,495 +903,406 @@
 s = 
-True%0A%09%09else:%0A%09%09%09options.indent_with_tabs = False%0A%0A%09%09if (%22preserve_newlines%22 in opts and opts%5B%22preserve_newlines%22%5D):%0A%09%09%09options.preserve_newlines = True%0A%09%09else:%0A%09%09%09options.preserve_newlines = False%0A%0A%09%09if (%22max_preserve_newlines%22 in opts and opts%5B%22max_preserve_newlines%22%5D):%0A%09%09%09options.max_preserve_newlines = opts%5B%22max_preserve_newlines
+opts%5B%22indent_with_tabs%22%5D%0A%0A%09%09if %22expand_tags%22 in opts:%0A%09%09%09options.expand_tags = opts%5B%22expand_tags%22%5D%0A%0A%09%09if %22expand_javascript%22 in opts:%0A%09%09%09options.expand_javascript = opts%5B%22expand_javascript
 %22%5D%0A
+%0A
 %09%09
-else:%0A%09%09%09options.max_preserve_newlines = 10%0A%0A%09%09if (%22indent_tags%22 in opts and opts%5B%22indent_tags%22%5D):%0A%09%09%09options.indent_tags = str(opts%5B%22indent_tags
+if %22reduce_empty_tags%22 in opts:%0A%09%09%09options.reduce_empty_tags = opts%5B%22reduce_empty_tags%22%5D%0A%0A%09%09if %22exception_on_tag_mismatch%22 in opts:%0A%09%09%09options.exception_on_tag_mismatch = opts%5B%22exception_on_tag_mismatch
 %22%5D
-)
 %0A%0A%09%09
@@ -1496,9 +1496,8 @@
  stderr%0A
-%0A

Add new HTMLBeautifier options