GithubHelp home page GithubHelp logo

emmetio / emmet Goto Github PK

View Code? Open in Web Editor NEW
4.4K 154.0 518.0 19.42 MB

The essential toolkit for web-developers

Home Page: http://emmet.io

License: MIT License

JavaScript 0.61% TypeScript 99.39%
emmet html abbreviation css

emmet's Introduction

Emmet — the essential toolkit for web-developers

Emmet 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:

<ul id="nav">
    <li class="item1"><a href="">Item 1</a></li>
    <li class="item2"><a href="">Item 2</a></li>
    <li class="item3"><a href="">Item 3</a></li>
    <li class="item4"><a href="">Item 4</a></li>
</ul>

Features

  • Familiar 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 type MyComponent>custom-element to convert any word into a tag.
  • CSS properties shortcuts: Emmet provides special syntax for CSS properties with embedded values. For example, bd1-s#f.5 will be expanded to border: 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 features

This repo contains only core module for parsing and expanding Emmet abbreviations. Editor plugins are available as separate repos.

This is a monorepo: top-level project contains all the code required for converting abbreviation into code fragment while ./packages folder contains modules for parsing abbreviations into AST and can be used independently (for example, as lexer for syntax highlighting).

Installation

You can install Emmet as a regular npm module:

npm i emmet

Usage

To expand abbreviation, pass it to default function of emmet module:

import expand from 'emmet';

console.log(expand('p>a')); // <p><a href=""></a></p>

By default, Emmet expands markup abbreviation, e.g. abbreviation used for producing nested elements with attributes (like HTML, XML, HAML etc.). If you want to expand stylesheet abbreviation, you should pass it as a type property of second argument:

import expand from 'emmet';

console.log(expand('p10', { type: 'stylesheet' })); // padding: 10px;

A 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 supports syntaxes with predefined snippets and options:

import expand from 'emmet';

console.log(expand('p10', { syntax: 'css' })); // padding: 10px;
console.log(expand('p10', { syntax: 'stylus' })); // padding 10px

Predefined syntaxes already have type attribute which describes whether given abbreviation is markup or stylesheet, but if you want to use it with your custom syntax name, you should provide type config option as well (default is markup):

import expand from 'emmet';

console.log(expand('p10', {
    syntax: 'my-custom-syntax',
    type: 'stylesheet',
    options: {
        'stylesheet.between': '__',
        'stylesheet.after': '',
    }
})); // padding__10px

You can pass options property as well to shape-up final output or enable/disable various features. See src/config.ts for more info and available options.

Extracting abbreviations from text

A 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 properly extracted from source code:

import expand, { extract } from 'emmet';

const source = 'Hello world ul.tabs>li';
const data = extract(source, 22); // { abbreviation: 'ul.tabs>li' }

console.log(expand(data.abbreviation)); // <ul class="tabs"><li></li></ul>

The extract function 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 with abbreviation property and start and end properties 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. The extract function is able to handle such cases with lookAhead option (enabled by default). This this option enabled, extract method automatically detects auto-inserted characters and adjusts location, which will be available as end property of the returned result:

import { extract } from 'emmet';

const source = 'a div[title] b';
const loc = 11; // right after "title" word

// `lookAhead` is enabled by default
console.log(extract(source, loc)); // { abbreviation: 'div[title]', start: 2, end: 12 }
console.log(extract(source, loc, { lookAhead: false })); // { abbreviation: 'title', start: 6, end: 11 }

By default, extract tries to detect markup abbreviations (see above). stylesheet abbreviations has slightly different syntax so in order to extract abbreviations for stylesheet syntaxes like CSS, you should pass type: 'stylesheet' option:

import { extract } from 'emmet';

const source = 'a{b}';
const loc = 3; // right after "b"

console.log(extract(source, loc)); // { abbreviation: 'a{b}', start: 0, end: 4 }


// Stylesheet abbreviations does not have `{text}` syntax
console.log(extract(source, loc, { type: 'stylesheet' })); // { abbreviation: 'b', start: 2, end: 3 }

Extract abbreviation with custom prefix

Lots of developers uses React (or similar) library for writing UI code which mixes JS and XML (JSX) in the same source code. Since any Latin 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.:

var div // `div` is a valid abbreviation, Emmet may transform it to `<div></div>`

A possible solution for this problem it to use prefix for abbreviation: abbreviation can be successfully extracted only if its preceded with given prefix.

import { extract } from 'emmet';

const source1 = '() => div';
const source2 = '() => <div';

extract(source1, source1.length); // Finds `div` abbreviation
extract(source2, source2.length); // Finds `div` abbreviation too

extract(source1, source1.length, { prefix: '<' }); // No match, `div` abbreviation is not preceded with `<` prefix
extract(source2, source2.length, { prefix: '<' }); // Finds `div` since it preceded with `<` prefix

With prefix option, 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. A prefix may contain multiple character but the last one must be a character which is not part of Emmet abbreviation. Good candidates are <, &, (emoji or Unicode symbol) and so on.

emmet's People

Contributors

ana-scottlogic avatar anubhar avatar avigoldman avatar can-erturk avatar defreeart avatar dependabot[bot] avatar devanshj avatar doctorderek avatar docyx avatar grafst avatar jaywright31 avatar mateusvahl avatar princesseuh avatar robbevp avatar rzhao271 avatar scottwillmoore avatar sergeche avatar theperfectpunk avatar touchedthecode avatar troy351 avatar yukulele avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

emmet's Issues

TextMate plugin error

в плагине к TextMate нехватает zencoding внутри .tmbundle/Support/

TextMate : No module named zencoding

Hi,

I have an issue when trying to expand my zencode, it writes down in textmate :

Traceback (most recent call last):
Traceback (most recent call last):
File "/Users/franco/Library/Application Support/TextMate/Bundles/Zen Coding.tmbundle/Support/expand_abbreviation.py", line 7, in
import zencoding
ImportError: No module named zencoding

Anybody knows what is wrong ?

Preferences module

Create preferences module that provides general storage for Zen Coding module preferences and convenient methods for listing and modification

Support different syntaxes for bem-like expanding

I think it's not good that .block>.-element is counted as .block>.__element by default — there could be people that would want to use this syntax, so the Zen Coding should be less strict on that, however it would nice to have this behavior in settings.

Also, right now .block--mode is expanded to <div class="block--mode"></div>, however there are people that use this as modifier (like Nicolas Gallagher mentioned in his article), so it would be nice if .block--mode was treated like .block.block--mode as .block_mode works now.

However, I think that all those things must be configurable in settings, so someone could say that .-- is an element or the .__ is a modifier.

A few bugs to report

The match_pair action function defined in zen_actions.py contains a bug, referencing attributes on a tag with getitem syntax, eg tag['start']. The Tag class in html_matcher.py actually has 'start' as an attribute.

There's also a bug in the the format-css.py code, where the regex will deform the '{%::zen-caret::%}' when trying to put some whitespace between "prop: value"

ps. Thanks for ZC!

вложенные элементы в |bem фильтре

некорректно раскрывает аббревиатуру span>a.__child:
<span><a href="" class="container container_child"></a></span>

.__child раскрывает правильно:
<a href="" class="container__child"></a>

|t (trim) filter does not remove number markers inside custom attributes

Performing this abbreviation ol>li*>span{$#}+{ }+a[title=$#]{$#}|t on the following:

  1. list item one
  2. list item two
  3. list item three

produces this output:

<ol>
<li>
<span>list item one</span>
<a href="" title="1. list item one">list item one</a>
</li>
<li>
<span>list item two</span>
<a href="" title="2. list item two">list item two</a>
</li>
<li>
<span>list item three</span>
<a href="" title="3. list item three">list item three</a>
</li>
</ol>

Note that the |t (trim) filter did not remove the number marker inside the custom title attribute [].

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Expand Abbreviation not working the same after recent update

I've set my Aptana to look for updates on every startup with the url http://zen-coding.ru/eclipse/updates/. About 3 weeks ago I received an update through the http://zen-coding.ru/eclipse/updates/ site. After the update it's not working exactly the same anymore. I've only noticed one thing so far. I've setup some custom snippets.

If I type in blank and try to expand it should expand to target="_blank".
This works except for if I try this <a href=""blank|> where my cursor is the pipe. I try to expand and it does nothing.

NOTE: PIPE IS MY CURSOR LOCATION
doesn't work but used to
<a href=""blank|>
<a href="" blank| >
<td blank|>
does work
<a blank|href="">

This worked fine before the update. I can type it anywhere else and it works as long as its not within an html tag.

Below are the details from Aptana from the installation details screen
CURRENT VERSION
Zen Coding for Eclipse 0.8.0.201201232221 ru.zencoding.eclipse.feature.group
VERSION BEFORE UPDATE tried to revert but couldn't because 0.7.0 wasn't available
Zen Coding for Eclipse 0.7.0.201103132329

Filter for JSON

It'd be great if zen-coding could expand not only HTML etc., but JSON as well.

For example, from something>another{content}+lalala+lololo{blah} you could get

{
    "something":
    [
        {
          "another": "content"
        },
        {
          "lalala": |
        },
        {
            "lololo": "blah"
        }
    ]
}

or something similar.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Delegates for “Reflect CSS Value”

“Reflect CSS Value” action should be able to handle custom “reflect” delegates, available for extension. Particularly, gradient generator should provide its own delegate.

Name resolver

Next version of Zen Coding should allow users to skip obvious tag names from expression. For example, ul.nav>.item should be the same as ul.nav>li.item, tr>.cell as tr>.td.cell and so on. It should be a stand-alone module that can be used in extensions.

Plugin error in TextMate

The textmate expansions are throwing an error:

Traceback (most recent call last):
File "/Users/numan/Library/Application Support/TextMate/Pristine Copy/Bundles/Zen Coding.tmbundle/Support/expand_abbreviation.py", line 7, in
import zencoding
ImportError: No module named zencoding

What am I doing wrong?

Add support for adding text

I would like it to be possible to set the inner text of an element via zen coding.
I can think of 2 good ways of doing this:

  1. css like:
    div#res[content=some_text]
    which might have problems with spaces
  2. new syntax:
    div#res{some text can go here}

I would try to implement it but it's hard for me to follow up the way ZC works

Non existing vendor prefixed CSS properties

There is no -ms-box-shadow and no -ms-border-radius so it does not make sense to include those in the snippets for "bsha" and "brad". I would also suggest to use "bxsh+" and "brds+" to make it more consistent with other existing abbreviations and conventions.

Please extend the Zen Coding XSL shortcuts to include all xsl elements

The XSL shortcuts need to be extended to include ALL xsl elements.

Here's a few of suggestions that I thought would be a great and helpful addition to the xslt shortcut library...

tex --> xsl:text/xsl:text

com --> xsl:comment/xsl:comment

attr-set --> <xsl:attribute-set name="" use-attribute-sets="">/xsl:attribute-set

imp --> <xsl:import href=""/>

inc --> <xsl:include href=""/>

key --> <xsl:key name="" match="" use=""/>

Add support for Sublime Text 2

I love zencoding, love ST2, but they never seem to get past the first date. He buys her flowers, she says no, etc etc.

Please, help them get married and have lots of high-efficiency text-editing babies. Please.

filters package in zencoding causing import problems

The zencoding package is placed in one of my plugin folders. Importing zen_actions and zen_core works fine. The problem arises when either of this packages try to import zencoding.filters. So I spent the better part of yesterday trying to figure out what's going in. I latter discover some magic going on in filters.init.py. Don't fully understand it or why it's needed, but it's cause import problems. In fact, I can't import zencoding.filters. Strange and unusual since I don't have this problem with any other python package. I suspect the magic going on in filters.init.py is going to break my python packages.

Also in zen_actions.py, why are you import zencoding to get zen_core when both zen_core and zen_actions are in the same package? You are doing:

from zencoding import zen_core as zen_coding
from zencoding import html_matcher

when you could just do

import zen_core as zen_coding
import html_matcher

Anyway this import problems are cause problem in my application. Do I need to do a global import somewhere to avoid these problems? Am I missing something?

Python 3 support

It would be great to update the Python code to support Python 3.0+. While Python 2 is still widely used, it just feels wrong writing new code in it.

I'll send a pull request if I get anywhere with upgrading it.

Read abbreviations from css

Hi,

Here is my suggestion:

Read abbreviations from css file/css rules.
With this feature users can define one css lib and reuse code.

Like this:

#something{....}
    #something > ul{....}
        #something > ul > li {....}
            #something > ul > li > a{....}

Then i can use:

something

And expand to:

<div id="something">
    <ul>
        <li><a href=""></a></li>
    </ul>
</div>

I know that need more specs about how this works, and don't know if is possible (without years codding).

replace_variables call uses outdated settings dictionary

I discovered this problem while working on TEA for Espresso with Zen Coding 0.6. Say I wanted to switch to using a different language:

from zencoding import stparser
from zencoding import zen_core as zen
user_settings = {'variables': {'lang': 'ru'}}
zen.update_settings(stparser.get_settings(user_settings))

If I then run an abbreviation that uses the ${lang} variable, I'll still get 'en' (the default value) output. This is because the replace_abbreviation has the vars parameter pre-filled, and modifying the zen_settings variable after the fact doesn't change it. To fix the problem, I modified zen_core.py as follows:

Replacing lines 234-240
def replace_variables(text, vars=None):
"""
Replace variables like ${var} in string
@param text: str
@return: str
"""
if vars is None:
vars = zen_settings['variables']
return re.sub(r'${([\w-]+)}', lambda m: m.group(1) in vars and vars[m.group(1)] or m.group(0), text)

This way, zen_settings['variables'] is always the most recent version, and the user doesn't need to edit zen_settings.py to change them (they can instead override them).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.