Commit graph

18 commits

Author SHA1 Message Date
Niklas Fasching
5464ab37d2 Fix race condition surrounding global orgWriter
Since writers are normally only used synchronously (i.e. to write one document
at a time), we don't guard modifications to their internal
state (e.g. temporarily replacing the string.Builder in WriteNodesAsString)
against race conditions.

The package global `orgWriter` and corresponding use cases of it (`org.String`,
`$node.String`) break that pattern - the writer is potentially used from
multiple go routines at the same time. This results in race conditions that
manifest as error messages like e.g.

    could not write output: runtime error: invalid memory address or nil pointer dereference. Using unrendered content.

Additionally, since we catch panics in `Document.Write`, the corresponding
stack trace is lost and dependents of go-org never know what hit them.

As using a writer across simultaneously across go routines is not a standard
pattern, we'll sync the use of the global `orgWriter` instead of trying to make
the actual writer threadsafe; less code noise for the common use case.
2023-03-12 11:28:55 +01:00
Niklas Fasching
1849701ba7 Add support for latex blocks / environments
current support for latex fragments was inline only, i.e. lines containing block
elements (e.g. a line starting with `* `, i.e. a headline) will not be parsed
as part of the latex fragment but the respective block element. Parsing latex
blocks at the block level should fix that. Note that in any case we don't do
any processing and just emit the raw latex (leaving the rendering to e.g. js).
2022-11-02 14:16:57 +01:00
Niklas Fasching
b3edd6c182 Improve block parameter parsing
The actual parameter parsing logic in emacs org mode is quite complex [1]. All
we want for now is to handle parameter values containing spaces [2]. Splitting
on ` :` gets us close enough for now. As I'm very opposed to copying 100 lines
of parameter parsing logic just to get exports right let's wait for use cases -
no hurt in gathering requirements as we go.

[1] https://github.com/bzg/org-mode/blob/master/lisp/ob-core.el#L1481
[2] I never ran into such parameters before and wrongly assumed that splitting
on spaces would be enough. boy was i wrong - just look at that massive
function [1]! that's why we can't have nice things!
2021-04-11 21:52:12 +02:00
Niklas Fasching
55b149054d fuzz: Fix index out of range for results without content
Offending crasher:

"\n\n\n\n\n\n\n\n\n#+RESULTS:"
2020-06-28 21:26:55 +02:00
Niklas Fasching
7e69931976 html: Implement source block :exports parameter 2020-04-17 15:24:42 +02:00
Niklas Fasching
cd923ba41a Implement source block results
To support code block directives like :exports none we need context - i.e. we
need to have the block and it's results at once and can't just render them
independently.
2020-04-17 15:02:56 +02:00
Niklas Fasching
f6f4646d45 Implement result blocks 2020-04-17 14:54:02 +02:00
Niklas Fasching
d417c2a6dd Implement escaping in src (org only) and example blocks
inside src example blocks lines starting with `\s*,` are escaped - i.e. org
mode will not try to parse them as e.g. a headline. We don't want to render the
escape commata to html so let's take them out - and put them back in before
rendering to org. Doing it this way allows us to render them correctly even
when the input did not include them.

see https://orgmode.org/manual/Literal-Examples.html#Literal-Examples
2020-04-16 16:07:54 +02:00
Niklas Fasching
9a0a9c11eb Export WriteNodesAsString on writer interface
WriteNodesAsString is simple enough to implement but exposing it is helpful in
the implementation of extending writers and we don't aim to keep writer a small
interface so let's expose it.
2019-11-02 23:44:16 +01:00
Niklas Fasching
bd33e8885e Add String() method to Node interface
Being able to very easily get the original [1] Org mode content seems like
something that will come up quite often and is very little code.

[1] it's not really the original content, but rather the pretty printed version
of that - as the semantics don't change it shouldn't matter.
2019-01-06 20:50:02 +01:00
Niklas Fasching
148eef5aeb Refactor: Move some code
Example nodes are a kind of block and it's not much code so we can just put it
next to the Block code. Feels cleaner but doesn't change much.
2019-01-06 20:49:49 +01:00
Niklas Fasching
42dc70e7ad Support blocks with unindented content
list items only contain content that is indented to their respective
level. Except when that content is inside a block. To allow for this we have to
ignore the parentStop when parsing a block and just include everything until
the end of that block.
Can't think of any problems with this right now. Let's see if this comes
back to bite me.
2018-12-19 00:28:53 +01:00
Niklas Fasching
0eb3baf1bb Improve handling of elements containing raw text
While adding another test case from the goorgeous issues it became clear that
inline markup and html entity replacement were erronously applied to raw text
elements like inline code =foo=, src/example/export blocks, example lines,
etc.

To correctly handle those cases in both org and html exports a new
parseRawInline method had to be added.

Also some misc html export whitespace fixes and stuff
2018-12-17 13:40:15 +01:00
Niklas Fasching
53b61abfd0 Support html export blocks 2018-12-11 16:05:36 +01:00
Niklas Fasching
6637e63892 Fix multiline emphasis
I didn't have a test case for this and broke it when i introduced Line nodes to
support printing back to org mode. Oops
2018-12-10 17:53:12 +01:00
Niklas Fasching
8f06883ec5 Allow blocks to contain paragraphs and other top level elements 2018-12-03 16:59:01 +01:00
Niklas Fasching
592be07cfd Refactor space handling of writers
I went through the issues of goorgeous and picked a few that seemed easy enough
to add (and added some fore as todos for later). That helped a lot and showed
some bugs / edge cases that required changes.

- the org writer wrote a lot of eol spaces and just removed it whenever
  String() was actually called. That worked until now but did not bode with
  rendering an empty headline - by removing ALL eol space we would render "* "
  back as just "*" -> not a headline anymore.
- the html writer had some special handling for line spacing inside paragraphs
  and list items - with the introduction of more blocks we need that handling
  everywhere.
  As browsers / html renderers are nice enough to collapse whitespace (and
  especially collapse "\s*\n" into " ") we can just write out the newlines and
  let the renderer take care of the rest.
2018-12-02 23:34:21 +01:00
Niklas Fasching
fc982125c9 Restructure directory layout: org subpackage 2018-12-02 18:37:55 +01:00
Renamed from block.go (Browse further)