Replace artlebedev typograph with mdash
This commit is contained in:
		
							
								
								
									
										57
									
								
								Typograph.py
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								Typograph.py
									
									
									
									
									
								
							| @@ -1,7 +1,7 @@ | ||||
| import sublime | ||||
| import sublime_plugin | ||||
|  | ||||
| from .lib.artlebedev import RemoteTypograf | ||||
| from .lib.mdash import Typograph | ||||
|  | ||||
|  | ||||
| __author__ = 'Anton Vakhrushev' | ||||
| @@ -11,48 +11,55 @@ __email__ = 'anwinged@ya.ru' | ||||
| PLUGIN_NAME = 'Typograph' | ||||
|  | ||||
| DEFAULT_SETTINGS = { | ||||
|     'type': 'html', | ||||
|     'breaks': False, | ||||
|     'paragraphs': False, | ||||
|     'max_no_break': 3, | ||||
|     'rules' = {} | ||||
| } | ||||
|  | ||||
|  | ||||
| class TypographSelectionCommand(sublime_plugin.TextCommand): | ||||
|     """ | ||||
|     Process selections of html text | ||||
|     """ | ||||
|     """Process selections of html text""" | ||||
|  | ||||
|     def run(self, edit): | ||||
|         """Executes command""" | ||||
|         settings = self.__get_settings() | ||||
|         rules = settings.get('rules', {}) | ||||
|  | ||||
|         settings = self.get_settings() | ||||
|  | ||||
|         typograf = RemoteTypograf() | ||||
|         typograf.htmlEntities() | ||||
|         typograf.br(settings['breaks']) | ||||
|         typograf.p(settings['paragraphs']) | ||||
|         typograf.nobr(settings['max_no_break']) | ||||
|         typograph = Typograph(rules) | ||||
|  | ||||
|         for region in self.view.sel(): | ||||
|             processed = typograf.processText(self.view.substr(region)) | ||||
|             self.view.replace(edit, region, processed.strip()) | ||||
|             regionText = self.view.substr(region) | ||||
|             processed = typograph.process(regionText).strip() | ||||
|             self.view.replace(edit, region, processed) | ||||
|  | ||||
|     def __get_settings(self): | ||||
|         """Loads plugin settings""" | ||||
|         local_settings = self.__get_local_settings() | ||||
|         project_settings = self.__get_project_settings() | ||||
|         self.__merge_settings(local_settings, project_settings) | ||||
|         return local_settings | ||||
|  | ||||
|     def get_settings(self): | ||||
|         """ | ||||
|         Load plugin settings | ||||
|         """ | ||||
|         settings = sublime.load_settings('{}.sublime-settings'.format(PLUGIN_NAME)) | ||||
|         project_settings = self.view.settings().get(PLUGIN_NAME, {}) | ||||
|     def __get_local_settings(self): | ||||
|         """Returns local settings""" | ||||
|         filename = self.__get_settings_filename() | ||||
|         settings = sublime.load_settings(filename) | ||||
|         local_settings = DEFAULT_SETTINGS.copy() | ||||
|  | ||||
|         for key in DEFAULT_SETTINGS: | ||||
|             local_settings[key] = settings.get(key) | ||||
|  | ||||
|         return local_settings | ||||
|  | ||||
|     def __get_project_settings(self): | ||||
|         """Return project settings""" | ||||
|         return self.view.settings().get(PLUGIN_NAME, {}) | ||||
|  | ||||
|     def __get_settings_filename(self): | ||||
|         """Returns plugin settings filename""" | ||||
|         return '{}.sublime-settings'.format(PLUGIN_NAME) | ||||
|  | ||||
|     def __merge_settings(self, local_settings, project_settings): | ||||
|         """Overrides local settings with project settings""" | ||||
|         for key in project_settings: | ||||
|             if key in DEFAULT_SETTINGS: | ||||
|                 local_settings[key] = project_settings[key] | ||||
|             else: | ||||
|                 print('{}: invalid key "{}" in project settings'.format(PLUGIN_NAME, key)) | ||||
|  | ||||
|         return local_settings | ||||
|   | ||||
| @@ -1,111 +0,0 @@ | ||||
| """ | ||||
| remotetypograf.py | ||||
| python-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client) | ||||
|  | ||||
| Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/ | ||||
|  | ||||
| Typograf homepage: http://typograf.artlebedev.ru/ | ||||
| Web-service address: http://typograf.artlebedev.ru/webservices/typograf.asmx | ||||
| WSDL-description: http://typograf.artlebedev.ru/webservices/typograf.asmx?WSDL | ||||
|  | ||||
| Default charset: UTF-8 | ||||
|  | ||||
| Python version | ||||
| Author: Sergey Lavrinenko (s.lavrinenko@gmail.com) | ||||
| Version: 1.0 (2007-05-18) based on script by Andrew Shitov (ash@design.ru) | ||||
|  | ||||
| Example: | ||||
|     from RemoteTypograf import RemoteTypograf | ||||
|         rt = RemoteTypograf() | ||||
|     # rt = RemoteTypograf('windows-1251') | ||||
|     print rt.processText ('"Вы все еще кое-как верстаете в "Ворде"? - Тогда мы идем к вам!"'); | ||||
| """ | ||||
|  | ||||
|  | ||||
| import socket | ||||
|  | ||||
|  | ||||
| class RemoteTypograf: | ||||
|  | ||||
|     _entityType = 4 | ||||
|     _useBr = 1 | ||||
|     _useP = 1 | ||||
|     _maxNobr = 3 | ||||
|     _encoding = 'UTF-8' | ||||
|  | ||||
|     def __init__(self, encoding='UTF-8'): | ||||
|         self._encoding = encoding | ||||
|  | ||||
|     def htmlEntities(self): | ||||
|         self._entityType = 1 | ||||
|  | ||||
|     def xmlEntities(self): | ||||
|         self._entityType = 2 | ||||
|  | ||||
|     def mixedEntities(self): | ||||
|         self._entityType = 4 | ||||
|  | ||||
|     def noEntities(self): | ||||
|         self._entityType = 3 | ||||
|  | ||||
|     def br(self, value): | ||||
|         self._useBr = 1 if value else 0 | ||||
|  | ||||
|      | ||||
|     def p(self, value): | ||||
|         self._useP = 1 if value else 0 | ||||
|  | ||||
|     def nobr(self, value): | ||||
|             self._maxNobr = int(value) if value else 0 | ||||
|  | ||||
|     def processText(self, text): | ||||
|  | ||||
|         text = text.replace('&', '&') | ||||
|         text = text.replace('<', '<') | ||||
|         text = text.replace ('>', '>') | ||||
|  | ||||
|         SOAPBody  = '<?xml version="1.0" encoding="UTF-8"?>\n' | ||||
|         SOAPBody += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\n' | ||||
|         SOAPBody += '<soap:Body>\r\n' | ||||
|         SOAPBody += ' <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">\n' | ||||
|         SOAPBody += '  <text>%s</text>\n' % text | ||||
|         SOAPBody += '     <entityType>%s</entityType>\n' % self._entityType | ||||
|         SOAPBody += '     <useBr>%s</useBr>\n' % self._useBr | ||||
|         SOAPBody += '     <useP>%s</useP>\n' % self._useP | ||||
|         SOAPBody += '     <maxNobr>%s</maxNobr>\n' % self._maxNobr  | ||||
|         SOAPBody += '   </ProcessText>\n' | ||||
|         SOAPBody += ' </soap:Body>\n' | ||||
|         SOAPBody += '</soap:Envelope>\n' | ||||
|  | ||||
|         host = 'typograf.artlebedev.ru'; | ||||
|         SOAPRequest  = 'POST /webservices/typograf.asmx HTTP/1.1\n' | ||||
|         SOAPRequest += 'Host: typograf.artlebedev.ru\n' | ||||
|         SOAPRequest += 'Content-Type: text/xml\n' | ||||
|         SOAPRequest += 'Content-Length: %d\n' % len(bytearray(SOAPBody, 'utf-8')) | ||||
|         SOAPRequest += 'SOAPAction: "http://typograf.artlebedev.ru/webservices/ProcessText"\n\n' | ||||
|  | ||||
|         SOAPRequest += SOAPBody | ||||
|  | ||||
|         remoteTypograf = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||||
|         remoteTypograf.connect((host, 80)) | ||||
|         remoteTypograf.sendall(bytearray(SOAPRequest, 'utf-8')) | ||||
|  | ||||
|         typografResponse = bytearray() | ||||
|         while 1: | ||||
|             buf = remoteTypograf.recv(8192) | ||||
|             if len(buf) == 0: break | ||||
|             typografResponse += buf | ||||
|  | ||||
|         remoteTypograf.close() | ||||
|          | ||||
|         typografResponse = typografResponse.decode(encoding='utf-8') | ||||
|  | ||||
|         startsAt = typografResponse.find('<ProcessTextResult>') + 19 | ||||
|         endsAt = typografResponse.find('</ProcessTextResult>') | ||||
|         typografResponse = typografResponse[startsAt:endsAt] | ||||
|  | ||||
|         typografResponse = typografResponse.replace('&', '&' ) | ||||
|         typografResponse = typografResponse.replace('<', '<') | ||||
|         typografResponse = typografResponse.replace ('>', '>') | ||||
|          | ||||
|         return typografResponse | ||||
							
								
								
									
										65
									
								
								lib/mdash.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								lib/mdash.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| import json | ||||
| import urllib.request | ||||
|  | ||||
|  | ||||
| __author__ = 'Anton Vakhrushev' | ||||
| __email__ = 'anwinged@ya.ru' | ||||
|  | ||||
|  | ||||
| MDASH_URL = 'http://mdash.ru/api.v1.php' | ||||
|  | ||||
|  | ||||
| class TypographError(RuntimeError): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class Typograph(object): | ||||
|  | ||||
|     def __init__(self, rules = None): | ||||
|         self.rules = self.__prepare_rules(rules) | ||||
|  | ||||
|     def process(self, text): | ||||
|         data = self.__prepare_data(text) | ||||
|         answer = self.__request(data) | ||||
|         answer = json.loads(answer) | ||||
|         self.__ensure_answer_is_correct(answer) | ||||
|         return answer['result'] | ||||
|  | ||||
|     def __prepare_data(self, text): | ||||
|         data = {} | ||||
|         data.update(self.rules) | ||||
|         data.update({'text': text}) | ||||
|         return data | ||||
|  | ||||
|     def __request(self, data): | ||||
|         data = urllib.parse.urlencode(data) | ||||
|         data = data.encode('utf-8') | ||||
|         request = urllib.request.Request(MDASH_URL, data) | ||||
|         responce = urllib.request.urlopen(request) | ||||
|         return responce.read().decode('utf-8') | ||||
|  | ||||
|     def __ensure_answer_is_correct(self, answer): | ||||
|         if answer.get('status') == 'error': | ||||
|             raise TypographError() | ||||
|  | ||||
|     def __prepare_rules(self, params): | ||||
|  | ||||
|         def convert(v): | ||||
|             return 'on' if v == 'on' or v == True else 'off' | ||||
|  | ||||
|         result = {} | ||||
|         params = params or {} | ||||
|  | ||||
|         for k in params: | ||||
|             result[k] = convert(params[k]) | ||||
|  | ||||
|         return result | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     params = { | ||||
|         'Text.paragraphs': False, | ||||
|         'Text.breakline': 'off', | ||||
|     } | ||||
|     t = Typograph(params) | ||||
|     print(t.process('"Вы все еще кое-как верстаете в "Ворде"? - Тогда мы идем к вам!"')) | ||||
		Reference in New Issue
	
	Block a user