Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: elixir
elixir: '1.5.2'
elixir: '1.8.1'
otp_release: '21.3'
script:
- "mix test --trace"
- "mix dialyzer"
- "mix dialyzer"
16 changes: 16 additions & 0 deletions lib/org/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ defmodule Org.Document do
%Org.Document{doc | sections: [Org.Section.prepend_content(current_section, content) | rest]}
end


@doc ~S"""
Prepend property to the currently deepest section.

While preserving order is usually not needed for parsing and
interpreting properties, order is still preserved here to e.g. allow
re-serialization that preserves line order. This would be desirable
e.g. since version control is often based on lines, and works better
if there is less noise in the commit history.

See prepend_content for usage.
"""
def prepend_property(%Org.Document{sections: [current_section | rest]} = doc, property) do
%Org.Document{doc | sections: [Org.Section.prepend_property(current_section, property) | rest]}
end

@doc ~S"""
Update the last prepended content. Yields the content to the given updater.

Expand Down
17 changes: 16 additions & 1 deletion lib/org/lexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Org.Lexer do

@type t :: %Org.Lexer{
tokens: list(token),
mode: :normal | :raw
mode: :normal | :raw | :property
}

@moduledoc ~S"""
Expand Down Expand Up @@ -58,6 +58,9 @@ defmodule Org.Lexer do
@section_title_re ~r/^(\*+) (.+)$/
@empty_line_re ~r/^\s*$/
@table_row_re ~r/^\s*(?:\|[^|]*)+\|\s*$/
@begin_props_re ~r/^\s*\:PROPERTIES\:$/
@property_re ~r/^\s*\:([A-Za-z]+)\:\s*(.+)$/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I know you added _ here on your repo as well. But I was wondering: should the set of characters really be limited?
I didn't find any mention of what valid properties are, except that they start and end with a colon.

Judging by what gets highlighted, it doesn't seem like there's any restriction (not even spaces):
2019-09-13-121131_301x97_scrot

So maybe \:([^\:]+)\: is safer here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the best case, we also respect rules about inherited properties, allowed values or adding to properties [1]. However, limiting the character set was just an arbitrary decision on my part after something -- but I didn't note down what it was -- didn't work as I expected, and I'd agree it is probably an unnecessary restriction. I searched for a while what characters could be used, but didn't find anything explicit, and so I deduced from the examples in the manual.

Do you think as a first step it would be okay if we don't give special treatment to '+' suffixes [1], special properties [2], etc.? In that case, I'd make the change you describe and leave additional refinement to the next person who needs it enough

[1] https://orgmode.org/manual/Property-syntax.html#Property-syntax
[2] https://orgmode.org/manual/Special-properties.html#Special-properties

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be incorrect syntax in your test case, the properties must be in a drawer.

* Test
:PROPERTIES:
:property-with-dashes: 123
:property with spaces: 123
:123278&%#@!#^$(#$: 345
:END:

whether placed in the property drawer or not, these properties are not presented in the org-mode
APIs, org-entry-properties or lower-level org-get-property-block which is specifically looking
for these drawers, using the regular expression stored in org-property-drawer-re.

org-entry-properties uses re-search-forward to match within that property block using org-property-re

Consider the following org-mode document:

#+begin_src emacs-lisp :results none
(highlight-regexp org-property-drawer-re 'hi-yellow)
(highlight-regexp org-property-re 'hi-pink)
(defun tmp/test-get-block ()
  (interactive)
  (let ((block (org-get-property-block)))
    (message "%s" (org-entry-properties))
    (message "%s" (if-let ((beg (when block (car block)))
                           (end (when block (cdr block))))
                      (buffer-substring beg end)
                    "NOPE"))))
#+end_src

* Test
:property-with-dashes: 123
:property with spaces: 123
:123278&%#@!#^$(#$: 345

* Test
:PROPERTIES:
:property-with-dashes: 123
:property with spaces: 123
:123278&%#@!#^$(#$: 345
:END:

* Test
:PROPERTIES:
:property-with-dashes: 123
:property with spaces: 123
:END:

* Test
:PROPERTIES:
:property-with-dashes: 123
:123278&%#@!#^$(#$: 345
:END:

* Test
:PROPERTIES:
:property-with-dashes: 123
:END:

* Test
:PROPERTIES:
:property_with_undies: 123
:END:

Executing the first highlight-regexp with C-x C-e at the end of it shows the last three headings
being matched as property blocks; the first three will not even consider them in
org-entry-properties. Going further, the second line will show which properties will be exported
within those blocks. This can be verified using the included function, which writes to the
*Messages* buffer the entry under the cursor's properties as returned by the high level APIs, as well as the area of the buffer returned by org-get-property-block, or "NOPE" if none match.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that may've been a bit unconstructive. I was looking around GitHub at org-mode parsers for a side-project and got nerd sniped. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skimming this, I think it is definitely constructive. Thanks! I've only been using this branch a little so far, and I also don't know that much about org mode syntax yet. I have my use cases, and that's that.

I'll look at your comment in detail, and hopefully come around to change this PR soon. If you found something better that you'd rather use to access org files from Elixir, do let me know ☺

@end_drawer_re ~r/^\s*\:END\:$/

defp lex_line(line, %Org.Lexer{mode: :normal} = lexer) do
cond do
Expand All @@ -78,6 +81,8 @@ defmodule Org.Lexer do
|> List.flatten
|> Enum.map(&String.trim/1)
append_token(lexer, {:table_row, cells})
Regex.run(@begin_props_re, line) ->
append_token(lexer, {:begin_drawer, "PROPERTIES"}) |> set_mode(:property)
true ->
append_token(lexer, {:text, line})
end
Expand All @@ -91,6 +96,16 @@ defmodule Org.Lexer do
end
end

defp lex_line(line, %Org.Lexer{mode: :property} = lexer) do
cond do
Regex.run(@end_drawer_re, line) ->
append_token(lexer, {:end_drawer}) |> set_mode(:normal)
match = Regex.run(@property_re, line) ->
[_, key, value] = match
append_token(lexer, {:property, key, value})
end
end

defp append_token(%Org.Lexer{} = lexer, token) do
%Org.Lexer{lexer | tokens: [token | lexer.tokens]}
end
Expand Down
16 changes: 15 additions & 1 deletion lib/org/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Org.Parser do

@type t :: %Org.Parser{
doc: Org.Document.t,
mode: :paragraph | :table | :code_block | nil,
mode: :paragraph | :properties | :table | :code_block | nil,
}

@moduledoc ~S"""
Expand Down Expand Up @@ -88,4 +88,18 @@ defmodule Org.Parser do
defp parse_token({:end_src}, %Org.Parser{mode: :code_block} = parser) do
%Org.Parser{parser | mode: nil}
end

defp parse_token({:begin_drawer, "PROPERTIES"}, parser) do
%Org.Parser{parser | mode: :properties}
end

defp parse_token({:property, key, value}, %Org.Parser{mode: :properties} = parser) do
doc = Org.Document.prepend_property(parser.doc, {key |> String.to_atom(), value})

%Org.Parser{parser | doc: doc}
end

defp parse_token({:end_drawer}, %Org.Parser{mode: :properties} = parser) do
%Org.Parser{parser | mode: nil}
end
end
18 changes: 16 additions & 2 deletions lib/org/section.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Org.Section do
defstruct title: "", children: [], contents: []
defstruct title: "", children: [], contents: [], properties: []

@moduledoc ~S"""
Represents a section of a document with a title and possible contents & subsections.

Example:
iex> source = "* Hello\nWorld\n** What's up?\nNothing much.\n** How's it going?\nAll fine, whow are you?\n"
iex> source = "* Hello\nWorld\n** What's up?\n :PROPERTIES:\n :Register: non-formal\n :Intent: inquisitive\n :END:\nNothing much.\n** How's it going?\nAll fine, whow are you?\n"
iex> doc = Org.Parser.parse(source)
iex> section = Org.section(doc, ["Hello"])
iex> section.contents
Expand All @@ -14,12 +14,16 @@ defmodule Org.Section do
2
iex> for child <- section.children, do: child.title
["What's up?", "How's it going?"]
iex> subsection_with_props = Org.section(doc, ["Hello", "What's up?"])
iex> subsection_with_props.properties
[Register: "non-formal", Intent: "inquisitive"]
"""

@type t :: %Org.Section{
title: String.t,
children: list(Org.Section.t),
contents: list(Org.Content.t),
properties: list(Keyword.t),
}

def add_nested(parent, 1, child) do
Expand All @@ -39,6 +43,7 @@ defmodule Org.Section do
section |
children: Enum.reverse(Enum.map(section.children, &reverse_recursive/1)),
contents: Enum.reverse(Enum.map(section.contents, &Org.Content.reverse_recursive/1)),
properties: Enum.reverse(section.properties),
}
end

Expand Down Expand Up @@ -82,4 +87,13 @@ defmodule Org.Section do
def update_content(%Org.Section{children: [current_section | rest]} = section, updater) do
%Org.Section{section | children: [update_content(current_section, updater) | rest]}
end

@doc "Adds property to the last prepended section"
def prepend_property(%Org.Section{children: []} = section, property) do
%Org.Section{section | properties: [property | section.properties]}
end

def prepend_property(%Org.Section{children: [current_child | children]} = section, property) do
%Org.Section{section | children: [prepend_property(current_child, property) | children]}
end
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ defmodule Org.Mixfile do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
{:ex_doc, "~> 0.16", only: :dev, runtime: false},
{:dialyxir, "~> 0.5.1"}
{:ex_doc, "~> 0.19", only: :dev, runtime: false},
{:dialyxir, "~> 1.0.0-rc.4", only: :dev, runtime: false}
]
end
end
12 changes: 9 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
%{"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.18.1", "37c69d2ef62f24928c1f4fdc7c724ea04aecfdf500c4329185f8e3649c915baf", [], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}}
%{
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.4", "71b42f5ee1b7628f3e3a6565f4617dfb02d127a0499ab3e72750455e986df001", [:mix], [{:erlex, "~> 0.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm"},
"erlex": {:hex, :erlex, "0.2.1", "cee02918660807cbba9a7229cae9b42d1c6143b768c781fa6cee1eaf03ad860b", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.3", "3c7b0f02851f5fc13b040e8e925051452e41248f685e40250d7e40b07b9f8c10", [:mix], [{:earmark, "~> 1.2", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
}
7 changes: 7 additions & 0 deletions test/org/lexer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ defmodule Org.LexerTest do
{:section_title, 2, "another"},
{:text, "2"},
{:section_title, 3, "thing"},
{:begin_drawer, "PROPERTIES"},
{:property, "Title", "Goldberg Variations"},
{:property, "Composer", "J.S. Bach"},
{:property, "Artist", "Glenn Gould"},
{:property, "Publisher", "Deutsche Grammophon"},
{:property, "NDisks", "1"},
{:end_drawer},
{:text, "3"},
{:section_title, 4, "is nesting"},
{:text, "4"},
Expand Down
10 changes: 10 additions & 0 deletions test/org/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@ defmodule Org.ParserTest do
%Org.CodeBlock{lang: "sql", details: "", lines: ["SELECT * FROM products;"]},
]
end

test "section with properties", %{doc: doc} do
assert Org.section(doc, ["Also", "another", "thing"]).properties == [
{:Title, "Goldberg Variations"},
{:Composer, "J.S. Bach"},
{:Artist, "Glenn Gould"},
{:Publisher, "Deutsche Grammophon"},
{:NDisks, "1"},
]
end
end
end
7 changes: 7 additions & 0 deletions test/org_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ defmodule OrgTest do
** another
2
*** thing
:PROPERTIES:
:Title: Goldberg Variations
:Composer: J.S. Bach
:Artist: Glenn Gould
:Publisher: Deutsche Grammophon
:NDisks: 1
:END:
3
**** is nesting
4
Expand Down