Skip to content

Commit

Permalink
feat: support rst experimentally (#4)
Browse files Browse the repository at this point in the history
* feat: support rst experimentally

* fix: these vars should have been used
  • Loading branch information
crispgm committed Aug 15, 2021
1 parent 48d9f80 commit 79f6c27
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ An extension for [telescope.nvim](https://github.com/nvim-telescope/telescope.nv

## Supported File Type

- Markdown (headings from `h1` to `h6`)
- Markdown
- AsciiDoc (experimental)
- OrgMode (experimental)
- ReStructuredText (experimental)

## Setup

Expand Down
6 changes: 6 additions & 0 deletions fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The format examples are referenced from:

- Markdown Example: <https://github.com/crispgm/crispgm.com/blob/master/site/_drafts/test-for-styles.md>
- AsciiDoc Example: <https://asciidoctor.org/docs/asciidoc-article/>
- OrgMode Example: <https://writequit.org/denver-emacs/presentations/files/example.org.html>
- ReStructuredText Example: <https://fangohr.github.io/computing/rst/rst.txt>
87 changes: 87 additions & 0 deletions fixtures/example.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
=========================================
ReStructuredText (rst): plain text markup
=========================================

.. sectnum::

.. contents:: The tiny table of contents

What is reStructuredText?
~~~~~~~~~~~~~~~~~~~~~~~~~

An easy-to-read, what-you-see-is-what-you-get plaintext markup syntax
and parser system, abbreviated *rst*. In other words, using a simple
text editor, documents can be created which

- are easy to read in text editor and
- can be *automatically* converted to

- html and
- latex (and therefore pdf)

What is it good for?
~~~~~~~~~~~~~~~~~~~~

reStructuredText can be used, for example, to

- write technical documentation (so that it can easily be offered as a
pdf file or a web page)

- create html webpages without knowing html

- to document source code

Show me some formatting examples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can highlight text in *italics* or, to provide even more emphasis
in **bold**. Often, when describing computer code, we like to use a
``fixed space font`` to quote code snippets.

We can also include footnotes [1]_. We could include source code files
(by specifying their name) which is useful when documenting code. We
can also copy source code verbatim (i.e. include it in the rst
document) like this::

int main ( int argc, char *argv[] ) {
printf("Hello World\n");
return 0;
}

We have already seen at itemised list in section `What is it good
for?`_. Enumerated list and descriptive lists are supported as
well. It provides very good support for including html-links in a
variety of ways. Any section and subsections defined can be linked to,
as well.


Where can I learn more?
~~~~~~~~~~~~~~~~~~~~~~~

reStructuredText is described at
http://docutils.sourceforge.net/rst.html. We provide some geeky small
print in this footnote [2]_.


Show me some more stuff, please
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can also include figures:

.. figure:: image.png
:width: 300pt


The magnetisation in a small ferromagnetic disk. The diametre is of the order of 120 nanometers and the material is Ni20Fe80. Png is a file format that is both acceptable for html pages as well as for (pdf)latex.

---------------------------------------------------------------------------

.. [1] although there isn't much point of using a footnote here.
.. [2] Random facts:
- Emacs provides an rst mode
- when converting rst to html, a style sheet can be provided (there is a similar feature for latex)
- rst can also be converted into XML
- the recommended file extension for rst is ``.txt``
91 changes: 91 additions & 0 deletions lua/telescope/_extensions/heading/format/rst.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
local ReStructuredText = {}

local function is_heading(line, char)
for i = 1, #line do
local c = line:sub(i, i)
if c ~= char then
return false
end
end

return true
end

function ReStructuredText.get_headings(filepath, start, total)
local headings = {}
local index = start
-- not all, but all recommended
local sectionChars = {
'=',
'-',
'`',
':',
'.',
"'",
'"',
'~',
'^',
'_',
'*',
'+',
'#',
}
local last_line = ''
local line_mode = 0 -- 1: overline, 2: underline
-- luacheck: no unused
local line_top = 0 -- for underline, there is no line_top, only line_bottom
local line_title = 0
local line_bottom = 0
local heading = ''
while index <= total do
local line = vim.fn.getline(index)
for _, char in pairs(sectionChars) do
if is_heading(line, char) then
if last_line == '' then -- overline start
line_mode = 1
line_top = string.len(line)
else
if line_mode == 1 then -- overline end
line_title = string.len(last_line)
line_bottom = string.len(line)
if
line_top
== line_title
and line_title
== line_bottom
then
heading = last_line
end
else -- underline
line_title = string.len(last_line)
line_bottom = string.len(line)
if line_title == line_bottom then
heading = last_line
line_mode = 2
end
end
end
if #heading > 0 then
table.insert(headings, {
heading = vim.trim(heading),
line = index,
path = filepath,
})
-- reset
heading = ''
line_mode = 0
line_top, line_bottom, line_title = 0, 0, 0
end

break
end
end

last_line = line
index = index + 1
end

return headings
end

return ReStructuredText

0 comments on commit 79f6c27

Please sign in to comment.