如何从组织模式 table 生成源代码?
How do I generate source code from an org mode table?
我正在尝试找到一种方法将我的 i3wm 配置文件移动到 org-mode 文件。我有一个带有键绑定的 table 以及它们应该执行什么命令,我想从中生成适当的源代码。
示例:
| Keybinding | Command | Action |
|-----------------------+----------------------+--------------------------|
| {{{mod}}} + Return | i3-sensible-terminal | Opens a new terminal |
| {{{mod}}} + Shift + q | kill | Kills the focused window |
应该生成
bindsym {{{mod}}}+Return exec --no-startup-id i3-sensible-terminal ;; Opens a new Terminal
bindsym {{{mod}}}+Shift+q exec --no-startup-id kill ;; Kills the focused window
这样的事情可能吗?
您可以命名 table 并将其作为参数传递给源块,并让源块遍历行。这是 python
中的一个实现:
#+NAME: commands
| Keybinding | Command | Action |
|-----------------------+----------------------+--------------------------|
| {{{mod}}} + Return | i3-sensible-terminal | Opens a new terminal |
| {{{mod}}} + Shift + q | kill | Kills the focused window |
#+begin_src python :var cmds=commands :results output raw
for row in cmds:
print("bindsym {} exec --no-startup-id {} ;; {}".format(row[0].replace(' ', ''), row[1], row[2]))
#+end_src
这里我假设第一列的空格应该去掉,而不是引用字符串,但是你可以很容易地修改它。
下面是 运行 上述源代码块的结果:
#+RESULTS:
bindsym {{{mod}}}+Return exec --no-startup-id i3-sensible-terminal ;; Opens a new terminal
bindsym {{{mod}}}+Shift+q exec --no-startup-id kill ;; Kills the focused window
我正在尝试找到一种方法将我的 i3wm 配置文件移动到 org-mode 文件。我有一个带有键绑定的 table 以及它们应该执行什么命令,我想从中生成适当的源代码。
示例:
| Keybinding | Command | Action |
|-----------------------+----------------------+--------------------------|
| {{{mod}}} + Return | i3-sensible-terminal | Opens a new terminal |
| {{{mod}}} + Shift + q | kill | Kills the focused window |
应该生成
bindsym {{{mod}}}+Return exec --no-startup-id i3-sensible-terminal ;; Opens a new Terminal
bindsym {{{mod}}}+Shift+q exec --no-startup-id kill ;; Kills the focused window
这样的事情可能吗?
您可以命名 table 并将其作为参数传递给源块,并让源块遍历行。这是 python
中的一个实现:
#+NAME: commands
| Keybinding | Command | Action |
|-----------------------+----------------------+--------------------------|
| {{{mod}}} + Return | i3-sensible-terminal | Opens a new terminal |
| {{{mod}}} + Shift + q | kill | Kills the focused window |
#+begin_src python :var cmds=commands :results output raw
for row in cmds:
print("bindsym {} exec --no-startup-id {} ;; {}".format(row[0].replace(' ', ''), row[1], row[2]))
#+end_src
这里我假设第一列的空格应该去掉,而不是引用字符串,但是你可以很容易地修改它。
下面是 运行 上述源代码块的结果:
#+RESULTS:
bindsym {{{mod}}}+Return exec --no-startup-id i3-sensible-terminal ;; Opens a new terminal
bindsym {{{mod}}}+Shift+q exec --no-startup-id kill ;; Kills the focused window