Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shira-3749 committed Nov 19, 2016
0 parents commit a67b55d
Show file tree
Hide file tree
Showing 104 changed files with 12,205 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/rstpad.local.pro
/rstpad.pro.user
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2016 Pavel Batečko (ShiraNai7)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
147 changes: 147 additions & 0 deletions README.rst
@@ -0,0 +1,147 @@
RSTPad
######

reStructuredText editor with live preview.

.. image:: screenshot.jpg

.. contents::


Features
********

- live preview with autoscroll

- configurable orientation (horizontal / vertical)

- search & replace

- toolbar with common elements:

- headings
- bold, italic
- inline code
- code blocks
- horizontal rule
- lists
- link
- image

- inserting highlighted code blocks (Pygments)

- autocomplete for known lexer names

- built-in help pages

- syntax cheat sheet
- directives

- export to PDF



Known limitations
*****************

- UTF-8 documents only


Implementation details
**********************

- the application contains an embedded Python interpreter

- Python libraries are located in the application's directory / bundle

- the Docutils library is used to convert RST to HTML on the fly

- the converstion is done in a separate thread
- *resources/preview/docutils_extensions.py* overrides some of the HTML writer's methods to insert additional tags and attributes

- HTML output is displayed using Qt WebEngine

- external links are delegated to QDesktopServices
- autoscroll is implemented in JavaScript
- the preview is "frozen" during loading and autoscrolling to allow for smooth transitions (a previously rendered frame is used for the duration)


Compilation
***********

Prerequisites
=============

- QT 5.7 & Qt Creator
- Python 2.7 (32bit or 64bit depending the used compiler)
- `Pygments <https://pypi.python.org/pypi/Pygments>`_ (tested with 2.1.3)
- `Docutils <https://sourceforge.net/p/docutils/code/HEAD/tree/trunk/docutils/>`_ (tested with 0.13, revision 7975)


Build configuration
===================

.. NOTE::
- OS X notes and examples assume that Python 2.7 has been installed using Homebrew.
- The paths used below (such as *C:/python*) are just examples. Use actual paths :)
- Use forward slashes in all paths. Omit trailing slashes.

Copy *rstpad.local.pro.dist*, name it *rstpad.local.pro* and set the following variables:

- ``PYTHON_LINK``

- Python linker flags
- Windows: ``-LC:/python/libs -lpython27``
- OS X: use the `python-config <http://manpages.ubuntu.com/manpages/precise/man1/python-config.1.html>`_ utility to determine linker flags:

- ``python2.7-config --ldflags``

- ``PYTHON_INCLUDE_PATH``

- path to a directory containing Python's header files

- Windows: ``C:/python/include``
- OS X: ``/usr/include/python2.7``

- ``PYTHON_SYS_PATH``

- path to a directory containing:

- ``libs.zip`` - the Python standard library as a ZIP archive
- ``pygments``, ``docutils`` - library directories (these cannot be inside *libs.zip*)
- any *.pyd*, *.dll* and *.so* modules that should be available in a standard Python installation

- *.pyc* files should be excluded


Windows deployment
==================

#. Use the `Windows Deployment Tool <http://doc.qt.io/qt-5/windows-deployment.html#the-windows-deployment-tool>`_ with the release binary to fetch required DLLs.

Example: ``windeployqt.exe --release --no-translations rstpad.exe``

#. Locate and copy the correct ``python27.dll`` file or the application won't work on machines without Python 2.7 installed.

- possible locations depend on how you've installed Python and on its version:

- *C:\\Windows\\System\\python27.dll*
- *C:\\Windows\\SysWoW64\\python27.dll*

- make sure you're ussing the correct version (32 vs 64bit)


OS X deployment
===============

Use the `Mac Deployment Tool <http://doc.qt.io/qt-5/osx-deployment.html#the-mac-deployment-tool>`_ to convert the resulting application into a *.dmg* file:

::

macdeployqt RSTPad.app -dmg


Other platforms
===============

Not tested yet. You're on your own :)
Binary file added resources/icons/arrow-right.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/bold.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/code-block-pygments.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/code-block.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/code.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/copy.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/cut.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/find-next.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/find-prev.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/find.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/floppy.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/folder.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/h1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/h2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/h3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/h4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/h5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/h6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/help.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/hr.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/info.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/italic.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/link.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/main.icns
Binary file not shown.
Binary file added resources/icons/main.ico
Binary file not shown.
Binary file added resources/icons/main.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/ol.png
Binary file added resources/icons/page.png
Binary file added resources/icons/paste.png
Binary file added resources/icons/pdf.png
Binary file added resources/icons/qt.png
Binary file added resources/icons/redo.png
Binary file added resources/icons/ul.png
Binary file added resources/icons/undo.png
Binary file added resources/icons/wrench.png
37 changes: 37 additions & 0 deletions resources/preview/docutils_extensions.py
@@ -0,0 +1,37 @@
global Node

import docutils.writers.html5_polyglot as html_writer
from docutils.nodes import Node

# store original methods
global orig_starttag
global orig_visit_document

orig_starttag = html_writer.HTMLTranslator.starttag
orig_visit_document = html_writer.HTMLTranslator.visit_document

# starttag() override
def starttag(self, node, tagname, suffix='\n', empty=False, **attributes):
# define the data-line attribute if this is a Node with a known line
line = None

if isinstance(node, Node):
line = node.line

if line is None and node.parent is not None:
line = node.parent.line

if line is not None:
attributes['data-line'] = str(node.line)

return orig_starttag(self, node, tagname, suffix, empty, **attributes)

# visit_document() override
def visit_document(self, node):
self.head.append('<link rel="stylesheet" type="text/css" href="qrc:///preview/preview.css">')
self.head.append('<script type="text/javascript" src="qrc:///preview/preview.js"></script>')
orig_visit_document(self, node)

# replace methods
html_writer.HTMLTranslator.starttag = starttag
html_writer.HTMLTranslator.visit_document = visit_document
39 changes: 39 additions & 0 deletions resources/preview/preview.css
@@ -0,0 +1,39 @@
/* Global */
body {
margin: 0 !important;
padding: 1em !important;
font-family: sans-serif;
}
h1:first-child {
margin-top: 0 !important;
}
.document {
max-width: 950px !important;
margin-left: 0 !important;
margin-right: 0 !important;
}
blockquote {
border-left: 4px solid #d5d5d5;
padding-left: 14px;
}

/* RST renderer exception */
.rst-exception {
margin: 40px;
border: medium outset;
padding-right: 1em;
padding-left: 1em;
}
.rst-exception pre {
overflow: auto;
}
.rst-exception-message {
white-space: pre-wrap;
font-weight: bold;
font-family: sans-serif;
color: red;
}
.rst-exception-trace {
font-size: smaller;
color: gray;
}
108 changes: 108 additions & 0 deletions resources/preview/preview.js
@@ -0,0 +1,108 @@
/**
* Logic invoked from C++ code
*/
class RstPadApi {
/**
* @param {Number} position
*/
static scrollToPosition(position) {
window.scrollTo(0, position);
}

/**
* @param {Number} line
*/
static scrollToLine(line) {
let position = this.getPositionForLine(line);

if (null !== position) {
this.scrollToPosition(position);
}
}

/**
* @param {Number} line
* @returns {Number|null}
*/
static getPositionForLine(line) {
let map = this.getLineMap();

//console.log('getPositionForLine ' + line);

// try to use element at the exact line
if (map[line]) {
//console.log('found exact element for line ' + line);
return this.getElementPosition(map[line]);
}

// find the nearest lines
let nearestPrevLine = null;
let nearestNextLine = null;

for (let mappedLine in map) {
if (map.hasOwnProperty(mappedLine)) {
mappedLine = mappedLine / 1;
//console.log('mappedLine = ' + mappedLine + '(' + typeof(mappedLine) + ') nearestPrevLine = ' + nearestPrevLine + ' nearestNextLine = ' + nearestNextLine);

if (mappedLine < line) {
if (null === nearestPrevLine || mappedLine > nearestPrevLine) {
nearestPrevLine = mappedLine;
}
} else if (null === nearestNextLine || mappedLine < nearestNextLine) {
nearestNextLine = mappedLine;
}
}
}

//console.log('nearestPrevLine = ' + nearestPrevLine + ' nearestNextLine = ' + nearestNextLine);

// estimate a position between the previous and next lines
if (nearestPrevLine && nearestNextLine) {
let prevElemPos = this.getElementPosition(map[nearestPrevLine], 'bottom');
let nextElemPos = this.getElementPosition(map[nearestNextLine], 'top');

//console.log('prevElemPos = ' + prevElemPos + ' nextElemPos = ' + nextElemPos);
//console.log('percentage = ' + ((line - nearestPrevLine) / (nearestNextLine - nearestPrevLine)));

if (prevElemPos < nextElemPos) {
return prevElemPos + (nextElemPos - prevElemPos) * (line - nearestPrevLine) / (nearestNextLine - nearestPrevLine);
}
}

// only one of the lines was found or the positions are unexpected
if (nearestPrevLine || nearestNextLine) {
//console.log('using one of the lines = ' + (nearestPrevLine || nearestNextLine));
return this.getElementPosition(map[nearestPrevLine || nearestNextLine]);
}

// failure
//console.log('failure');
return null;
}

/**
* @returns {Object} line => HTMLElement
*/
static getLineMap() {
if (!this._lineMap) {
this._lineMap = {}
let elements = document.querySelectorAll('*[data-line]');

for (let i = 0; i < elements.length; ++i) {
this._lineMap[elements[i].dataset.line] = elements[i];
}
}

return this._lineMap;
}

/**
* @param {HTMLElement} elem
* @param {String} type
* @returns {Number}
*/
static getElementPosition(elem, type = 'top') {
return window.pageYOffset + elem.getBoundingClientRect()[type];
}
}

15 changes: 15 additions & 0 deletions resources/preview/test.py
@@ -0,0 +1,15 @@
import os
import docutils.core

execfile(os.path.dirname(os.path.realpath(__file__)) + '/docutils_extensions.py')

source = """
Hello
=====
How are you
""";

print docutils.core.publish_string(source, None, None, None, 'standalone', None, 'restructuredtext', None, 'html5', None, None, {'output_encoding': 'unicode'})

0 comments on commit a67b55d

Please sign in to comment.