Package: py-emmet
Emmet — the essential toolkit for web-developersThis is the official Python port of original Emmet code base written in JavaScript:https://github.com/emmetio/emmetEmmet is a web-developer’s toolkit for boosting HTML & CSS code writing.With Emmet, you can type expressions (abbreviations) similar to CSS selectors and convert them into code fragment with a single keystroke. For example, this abbreviation:ul#nav>li.item$*4>a{Item $}...can be expanded into:<ulid="nav"><liclass="item1"><ahref="">Item 1</a></li><liclass="item2"><ahref="">Item 2</a></li><liclass="item3"><ahref="">Item 3</a></li><liclass="item4"><ahref="">Item 4</a></li></ul>FeaturesFamiliar syntax: as a web-developer, you already know how to use Emmet. Abbreviation syntax is similar to CSS Selectors with shortcuts for id, class, custom attributes, element nesting and so on.Dynamic snippets: unlike default editor snippets, Emmet abbreviations are dynamic and parsed as-you-type. No need to predefine them for each project, just typeMyComponent>custom-elementto convert any word into a tag.CSS properties shortcuts: Emmet provides special syntax for CSS properties with embedded values. For example,bd1-s#f.5will be exampled toborder: 1px solid rgba(255, 255, 255, 0.5).Available for most popular syntaxes: use single abbreviation to produce code for most popular syntaxes like HAML, Pug, JSX, SCSS, SASS etc.Read more about Emmet featuresInstallationYou can install Emmet as a regular pip module:pipinstallpy-emmetUsageTo expand abbreviation, pass it toexpandfunction ofemmetmodule:importemmetprint(emmet.expand('p>a'))# <p><a href=""></a></p>By default, Emmet expandsmarkupabbreviation, e.g. abbreviation used for producing nested elements with attributes (like HTML, XML, HAML etc.). If you want to expandstylesheetabbreviation, you should pass it as atypeproperty of second argument:importemmetprint(emmet.expand('p10',{'type':'stylesheet'}))# padding: 10pxA stylesheet abbreviation has slightly different syntax compared to markup one: it doesn’t support nesting and attributes but allows embedded values in element name.Alternatively, Emmet supportssyntaxeswith predefined snippets and options:importemmetprint(emmet.expand('p10',{'syntax':'css'}))# padding: 10px;print(emmet.expand('p10',{'syntax':'stylus'}))# padding 10pxPredefined syntaxes already havetypeattribute which describes whether given abbreviation is markup or stylesheet, but if you want to use it with your custom syntax name, you should providetypeconfig option as well (default ismarkup):importemmetprint(emmet.expand('p10',{'syntax':'my-custom-syntax','type':'stylesheet','options':{'stylesheet.between':'__','stylesheet.after':'',}}))# padding__10pxYou can passoptionsproperty as well to shape-up final output or enable/disable various features. Seeemmet/config.pyfor more info and available options.Extracting abbreviations from textA common workflow with Emmet is to type abbreviation somewhere in source code and then expand it with editor action. To support such workflow, abbreviations must be properlyextractedfrom source code:importemmetsource='Hello world ul.tabs>li'data=emmet.extract(source,22)# { abbreviation: 'ul.tabs>li' }print(emmet.expand(data.abbreviation))# <ul class="tabs"><li></li></ul>Theextractfunction accepts source code (most likely, current line) and character location in source from which abbreviation search should be started. The abbreviation is searched in backward direction: the location pointer is moved backward until it finds abbreviation bound. Returned result is an object withabbreviationproperty andstartandendproperties which describe location of extracted abbreviation in given source.Most current editors automatically insert closing quote or bracket for(,[and{characters so when user types abbreviation that uses attributes or text, it will end with the following state (|is caret location):ul>li[title="Foo|"]E.g. caret location is not at the end of abbreviation and must be moved a few characters ahead. Theextractfunction is able to handle such cases withlookAheadoption (enabled by default). This this option enabled,extractmethod automatically detects auto-inserted characters and adjusts location, which will be available asendproperty of the returned result:importemmetsource='a div[title] b'loc=11# right after "title" word# `lookAhead` is enabled by defaultprint(emmet.extract(source,loc))# { abbreviation: 'div[title]', start: 2, end: 12 }print(emmet.extract(source,loc,{'lookAhead':false}))# { abbreviation: 'title', start: 6, end: 11 }By default,extracttries to detectmarkupabbreviations (see above).stylesheetabbreviations has slightly different syntax so in order to extract abbreviations for stylesheet syntaxes like CSS, you should passtype: 'stylesheet'option:importemmetsource='a{b}'loc=3# right after "b"print(emmet.extract(source,loc));# { abbreviation: 'a{b}', start: 0, end: 4 }# Stylesheet abbreviations does not have `{text}` syntaxprint(emmet.extract(source,loc,{'type':'stylesheet'}));# { abbreviation: 'b', start: 2, end: 3 }Extract abbreviation with custom prefixLots of developers uses React (or similar) library for writing UI code which mixes JS and XML (JSX) in the same source code. SinceanyLatin word can be used as Emmet abbreviation, writing JSX code with Emmet becomes pain since it will interfere with native editor snippets and distract user with false positive abbreviation matches for variable names, methods etc.:vardiv// `div` is a valid abbreviation, Emmet may transform it to `<div></div>`A possible solution for this problem it to useprefixfor abbreviation: abbreviation can be successfully extracted only if its preceded with given prefix.importemmetsource1='() => div'source2='() => <div'emmet.extract(source1,len(source1))# Finds `div` abbreviationemmet.extract(source2,len(source2))# Finds `div` abbreviation tooemmet.extract(source1,len(source1),{'prefix':'<'})# No match, `div` abbreviation is not preceded with `<` prefixemmet.extract(source2,len(source2),{'prefix':'<'})# Finds `div` since it preceded with `<` prefixWithprefixoption, you can customize your experience with Emmet in any common syntax (HTML, CSS and so on) if user is distracted too much with Emmet completions for any typed word. Aprefixmay contain multiple character but the last onemustbe a character which is not part of Emmet abbreviation. Good candidates are<,&,→(emoji or Unicode symbol) and so on.