Esempio n. 1
0
def main():
    AsyncIOMainLoop().install()

    doc = get_document()

    if not options.config.no_default_js:
        doc.add_jsfile(
            'https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js')
        doc.add_jsfile('static/bootstrap.min.js')
    if not options.config.no_default_css:
        doc.add_cssfile('static/bootstrap.min.css')

    _user_static_dirs = set()
    for js in options.config.js_files:
        _user_static_dirs.add(path.dirname(js))
        doc.add_jsfile(js)
    for css in options.config.css_files:
        _user_static_dirs.add(path.dirname(css))
        doc.add_cssfile(css)

    # choices arg is better, but detecting error is not easy in livemark.vim
    if options.config.highlight_theme in STYLE_MAP:
        pygments_style = HtmlFormatter(
            style=options.config.highlight_theme).get_style_defs()
    else:
        pygments_style = HtmlFormatter('default').get_style_defs()
    doc.head.appendChild(Style(pygments_style))

    script = Script(parent=doc.body)
    script.innerHTML = '''
        function moveToElement(id) {
            var elm = document.getElementById(id)
            if (elm) {
                var x = window.scrollX
                var rect = elm.getBoundingClientRect()
                window.scrollTo(x, rect.top + window.scrollY)
                console.log(elm.textContent)
            }
        }
    '''
    mount_point = Div(parent=doc.body, class_='container')
    mount_point.appendChild(H2('LiveMark is running...'))
    app = get_app(doc)
    app.add_static_path('static', static_dir)
    for _d in _user_static_dirs:
        app.add_static_path(_d, _d)
    web_server = start_server(app, port=options.config.browser_port)

    loop = asyncio.get_event_loop()
    vim_server = Server(port=options.config.vim_port, loop=loop, doc=doc,
                        mount_point=mount_point)
    browser = webbrowser.get(options.config.browser)
    browser.open('http://localhost:{}'.format(options.config.browser_port))
    try:
        vim_server.start()
        loop.run_forever()
    except KeyboardInterrupt:
        vim_server.stop()
        web_server.stop()
Esempio n. 2
0
def get_new_document(include_rimo: bool = True,
                     include_skeleton: bool = False,
                     include_normalizecss: bool = False,
                     autoreload: Optional[bool] = None,
                     reload_wait: int = None,
                     log_level: int = None,
                     log_prefix: str = None,
                     log_console: bool = False,
                     ws_url: str = None,
                     document_factory: Callable[..., Document] = Document,
                     **kwargs) -> Document:
    document = document_factory(
        autoreload=autoreload,
        reload_wait=reload_wait,
        **kwargs)
    if log_level is None:
        log_level = config.logging

    log_script = []
    if log_level is not None:
        if isinstance(log_level, str):
            log_script.append('var RIMO_LOG_LEVEL = \'{}\''.format(log_level))
        elif isinstance(log_level, int):
            log_script.append('var RIMO_LOG_LEVEL = {}'.format(log_level))
    if log_prefix is not None:
        log_script.append('var RIMO_LOG_PREFIX = \'{}\''.format(log_prefix))
    if log_console:
        log_script.append('var RIMO_LOG_CONSOLE = true')
    if log_script:
        _s = Script(parent=document.head)
        _s.textContent = '\n{}\n'.format('\n'.join(log_script))

    if ws_url is not None:
        _s = Script(parent=document.head)
        _s.textContent = '\nvar RIMO_WS_URL = \'{}\'\n'.format(ws_url)

    if include_rimo:
        document.add_jsfile_head('_static/js/rimo/rimo.js')

    return document
Esempio n. 3
0
    def __init__(self,
                 *,
                 doctype: str = 'html',
                 title: str = 'W-DOM',
                 charset: str = 'utf-8',
                 default_class: type = WdomElement,
                 autoreload: bool = None,
                 reload_wait: float = None,
                 **kwargs: Any) -> None:
        """Create new document object for WDOM application.

        .. caution::

            Don't create new document from :class:`WdomDocument` class
            constructor. Use :func:`get_new_document` function instead.

        :arg str doctype: doctype of the document (default: html).
        :arg str title: title of the document.
        :arg str charset: charset of the document.
        :arg type default_class: Set default Node class of the document. This
            class is used when make node by :py:meth:`createElement()`
        :arg bool autoreload: Enable/Disable autoreload (default: False).
        :arg float reload_wait: How long (seconds) wait to reload. This
            parameter is only used when autoreload is enabled.
        """
        self.__tempdir = _tempdir = tempfile.mkdtemp()
        self._finalizer = weakref.finalize(
            self,  # type: ignore
            partial(_cleanup, _tempdir))
        self._autoreload = autoreload
        self._reload_wait = reload_wait

        super().__init__(doctype=doctype, default_class=default_class)
        self.characterSet = charset
        self.title = title
        self.script = Script(parent=self.body)
        self._autoreload_script = Script(parent=self.head)
        self.addEventListener('mount', self._on_mount)
Esempio n. 4
0
def get_new_document(  # noqa: C901
        include_wdom_js: bool = True,
        include_skeleton: bool = False,
        include_normalizecss: bool = False,
        autoreload: bool = None,
        reload_wait: float = None,
        log_level: Union[int, str] = None,
        log_prefix: str = None,
        log_console: bool = False,
        ws_url: str = None,
        message_wait: float = None,
        document_factory: Callable[..., Document] = WdomDocument,
        **kwargs: Any) -> Document:
    """Create new :class:`Document` object with options.

    :arg bool include_wdom_js: Include wdom.js file. Usually should be True.
    :arg bool include_skeleton: Include skelton.css.
    :arg bool include_normalizecss: Include normalize.css.
    :arg bool autoreload: Enable autoreload flag. This flag overwrites
        ``--debug`` flag, which automatically enables autoreload.
    :arg float reload_wait: Seconds to wait until reload when autoreload is
        enabled.
    :arg str log_level: Log level string, chosen from DEBUG, INFO, WARN, ERROR.
        Integer values are also acceptable like ``logging.INFO``. By default
        use ``wdom.config.options.log_level``, which default is ``INFO``.
    :arg str log_prefix: Prefix of log outputs.
    :arg bool log_console: Flag to show wdom log on browser console.
    :arg str ws_url: URL string to the ws url.
        Default: ``ws://localhost:8888/wdom_ws``.
    :arg float message_wait: Duration (seconds) to send WS messages.
    :arg Callable document_factory: Factory function/class to create Document
        object.
    :rtype: Document
    """
    document = document_factory(autoreload=autoreload,
                                reload_wait=reload_wait,
                                **kwargs)

    if log_level is None:
        log_level = config.logging
    if message_wait is None:
        message_wait = config.message_wait

    log_script = []
    log_script.append('var WDOM_MESSAGE_WAIT = {}'.format(message_wait))
    if isinstance(log_level, str):
        log_script.append('var WDOM_LOG_LEVEL = \'{}\''.format(log_level))
    elif isinstance(log_level, int):
        log_script.append('var WDOM_LOG_LEVEL = {}'.format(log_level))
    if log_prefix:
        log_script.append('var WDOM_LOG_PREFIX = \'{}\''.format(log_prefix))
    if log_console:
        log_script.append('var WDOM_LOG_CONSOLE = true')
    if log_script:
        _s = Script(parent=document.head)
        _s.textContent = '\n{}\n'.format('\n'.join(log_script))

    if ws_url:
        _s = Script(parent=document.head)
        _s.textContent = '\nvar WDOM_WS_URL = \'{}\'\n'.format(ws_url)

    if include_wdom_js:
        document.add_jsfile_head('_static/js/wdom.js')

    return document
Esempio n. 5
0
 def add_jsfile_head(self, src: str) -> None:
     """Add JS file to load at this document's header."""
     self.head.appendChild(Script(src=src))
Esempio n. 6
0
 def add_jsfile(self, src: str) -> None:
     """Add JS file to load at this document's bottom of the body."""
     self.body.appendChild(Script(src=src))
Esempio n. 7
0
from wdom.document import get_document
from wdom.server import start
from wdom.tag import Link, Script, Button

if __name__ == '__main__':
    document = get_document()
    # Add <link>-tag sourcing bootstrap.min.css on <head>
    document.head.appendChild(Link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'))

    # Add <script>-tag sourcing jquery and bootstrap.min.js to <body>
    document.body.appendChild(Script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'))
    document.body.appendChild(Script(src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'))

    # Add bootstrap button element
    document.body.appendChild(Button('click', class_='btn btn-primary'))

    start()
Esempio n. 8
0
def main():
    AsyncIOMainLoop().install()

    doc = get_document()

    if not options.config.no_default_js:
        doc.add_jsfile(
            'https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'
        )
        doc.add_jsfile('static/bootstrap.min.js')
    if not options.config.no_default_css:
        doc.add_cssfile('static/bootstrap.min.css')

    _user_static_dirs = set()
    for js in options.config.js_files:
        _user_static_dirs.add(path.dirname(js))
        doc.add_jsfile(js)
    for css in options.config.css_files:
        _user_static_dirs.add(path.dirname(css))
        doc.add_cssfile(css)

    # choices arg is better, but detecting error is not easy in livemark.vim
    if options.config.highlight_theme in STYLE_MAP:
        pygments_style = HtmlFormatter(
            style=options.config.highlight_theme).get_style_defs()
    else:
        pygments_style = HtmlFormatter('default').get_style_defs()
    doc.head.appendChild(Style(pygments_style))

    script = Script(parent=doc.body)
    script.innerHTML = '''
        function moveToElement(id) {
            var elm = document.getElementById(id)
            if (elm) {
                var x = window.scrollX
                var rect = elm.getBoundingClientRect()
                window.scrollTo(x, rect.top + window.scrollY)
                console.log(elm.textContent)
            }
        }
    '''
    mount_point = Div(parent=doc.body, class_='container')
    mount_point.appendChild(H2('LiveMark is running...'))
    app = get_app(doc)
    app.add_static_path('static', static_dir)
    for _d in _user_static_dirs:
        app.add_static_path(_d, _d)
    web_server = start_server(app, port=options.config.browser_port)

    loop = asyncio.get_event_loop()
    vim_server = Server(port=options.config.vim_port,
                        loop=loop,
                        doc=doc,
                        mount_point=mount_point)
    browser = webbrowser.get(options.config.browser)
    browser.open('http://localhost:{}'.format(options.config.browser_port))
    try:
        vim_server.start()
        loop.run_forever()
    except KeyboardInterrupt:
        vim_server.stop()
        web_server.stop()