From bea258a44e761fdb8a35273edebdd608785526f6 Mon Sep 17 00:00:00 2001 From: Shubham Kumar Date: Wed, 7 Feb 2024 08:21:49 +0530 Subject: [PATCH] Blog jmespath emacs library (#45) --- org-to-mdx.py | 13 ++-- org/2024-02-06-jmespath-emacs-library.org | 62 ++++++++++++++++++ .../2024-02-06-jmespath-emacs-library.mdx | 64 +++++++++++++++++++ 3 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 org/2024-02-06-jmespath-emacs-library.org create mode 100644 src/content/blog/2024-02-06-jmespath-emacs-library.mdx diff --git a/org-to-mdx.py b/org-to-mdx.py index 94a2429..5017fd2 100644 --- a/org-to-mdx.py +++ b/org-to-mdx.py @@ -66,7 +66,10 @@ def replace_image(match): # Convert code block to md code block elif org_line.startswith("#+BEGIN_SRC") or org_line.startswith("#+begin_src"): - md_content += "```" + org_line.split(" ")[1].split(" ")[0] + "\n" + if len(org_line.split(" ")) == 1: + md_content += "```" + "\n" + if len(org_line.split(" ")) > 1: + md_content += "```" + org_line.split(" ")[1].split(" ")[0] + "\n" in_code_block = True elif org_line.startswith("#+END_SRC") or org_line.startswith("#+end_src"): md_content += "```\n" @@ -87,6 +90,8 @@ def replace_image(match): # images, contains .png, .jpg, .jpeg, .gif elif re.search(r'\[\[([^\]]+)\]\]', org_line): md_content += re.sub(r'\[\[([^\]]+)\]\]', replace_image, org_line) + "\n" + elif re.search(r'.*\[\[(.*?)\]\[(.*?)\]\].*', org_line): + md_content += re.sub(r"\[\[(.*?)\]\[(.*?)\]\]", lambda x: "[" + x.group(2) + "](" + x.group(1) + ")", org_line) # Normal line else: # Bold @@ -95,10 +100,10 @@ def replace_image(match): org_line = re.sub(r"/(.*?)/", lambda x: "*" + x.group(1) + "*", org_line) # Underline org_line = re.sub(r"_(.*?)_", lambda x: "" + x.group(1) + "", org_line) - # Strikethrough - org_line = re.sub(r"~(.*?)~", lambda x: "~~" + x.group(1) + "~~", org_line) + # Inline code block + org_line = re.sub(r"~(.*?)~", lambda x: "`" + x.group(1) + "`", org_line) # Link - org_line = re.sub(r"\[\[(.*?)\]\[(.*?)\]\]", lambda x: "[" + x.group(2) + "](" + x.group(1) + ")", org_line) + # org_line = re.sub(r"\[\[(.*?)\]\[(.*?)\]\]", lambda x: "[" + x.group(2) + "](" + x.group(1) + ")", org_line) # Table org_line = re.sub(r"\|", lambda x: " | ", org_line) # Checkbox diff --git a/org/2024-02-06-jmespath-emacs-library.org b/org/2024-02-06-jmespath-emacs-library.org new file mode 100644 index 0000000..81760fe --- /dev/null +++ b/org/2024-02-06-jmespath-emacs-library.org @@ -0,0 +1,62 @@ +#+title: Using Jmespath in Emacs +#+AUTHOR: Shubham Kumar +#+DATE: 2024-02-06 +#+PROPERTY: description: Querying JSON files/data using Jmespath library in Emacs +#+PROPERTY: draft: false +#+PROPERTY: heroImage: /hero-images/default.png +#+PROPERTY: ideaDate: Feb 06, 2024 + +* Introduction + +When you have a small JSON file, it is quite easy to look for what you want. +But querying a large JSON data is very troublesome. + +This is where people use tools like Jmespath to filter and transform data into their liking. +This post is about a small wrapper over `jp` CLI utility that you can use while working on JSON files in Emacs. + +* Installing JP + +** Linux + +On Linux, you can install the utility by first downloding the binary and then installing it. +#+begin_src bash +sudo wget https://github.com/jmespath/jp/releases/latest/download/jp-linux-amd64 \ + -O /usr/local/bin/jp && sudo chmod +x /usr/local/bin/jp +#+end_src + +** Mac +On Mac, you can install Jmespath CLI using `brew`. +#+begin_src bash +brew install jmespath/jmespath/jp +#+end_src + +* Adding Jmespath recipe in Emacs + +This is my first recipe that I published on [[https://melpa.org/][MELPA]]. +Here are the steps to install it in ~Doom~ using ~straight~. +You can use any package manager to install it using ~MELPA~ repository. + +** Doom Emacs + +1. On Doom, you can just mention the below recipe in your ~package.el~. +#+begin_src elisp +(package! jmespath) +#+end_src + +2. Also, add the below line in your ~config.el~. +#+begin_src elisp +(use-package jmespath) +#+end_src + +* Using Jmespath + +There is an interactive function, ~jmespath-query-and-show~ that you can use to query the currently opened buffer or a file. +To use it with current buffer, you can simply call this function and enter your query. +The output will be shown on a new buffer with name *JMESPath Result*. + +To use this with a different file, you can set the ~Universal Argument~ using ~C-u~ or ~SPC-u~ (using evil). +Then you can enter the file to execute the query on. + +* Read more + +To learn more about JMESPath, visit [[https://jmespath.org/][Jamespath page]]. diff --git a/src/content/blog/2024-02-06-jmespath-emacs-library.mdx b/src/content/blog/2024-02-06-jmespath-emacs-library.mdx new file mode 100644 index 0000000..59e6d65 --- /dev/null +++ b/src/content/blog/2024-02-06-jmespath-emacs-library.mdx @@ -0,0 +1,64 @@ +--- +title: Using Jmespath in Emacs +AUTHOR: Shubham Kumar +DATE: 2024-02-06 +description: Querying JSON files/data using Jmespath library in Emacs +draft: false +heroImage: /hero-images/default.png +ideaDate: Feb 06, 2024 +--- + + +## Introduction + +When you have a small JSON file, it is quite easy to look for what you want. +But querying a large JSON data is very troublesome. + +This is where people use tools like Jmespath to filter and transform data into their liking. +This post is about a small wrapper over `jp` CLI utility that you can use while working on JSON files in Emacs. + +## Installing JP + +### Linux + +On Linux, you can install the utility by first downloding the binary and then installing it. +```bash +> sudo wget https://github.com/jmespath/jp/releases/latest/download/jp-linux-amd64 \ +> -O /usr/local/bin/jp && sudo chmod +x /usr/local/bin/jp +``` + +### Mac +On Mac, you can install Jmespath CLI using `brew`. +```bash +> brew install jmespath/jmespath/jp +``` + +## Adding Jmespath recipe in Emacs + +This is my first recipe that I published on [MELPA](https://melpa.org/).Here are the steps to install it in `Doom` using `straight`. +You can use any package manager to install it using `MELPA` repository. + +### Doom Emacs + +1. On Doom, you can just mention the below recipe in your `package.el`. +```elisp +> (package! jmespath) +``` + +2. Also, add the below line in your `config.el`. +```elisp +> (use-package jmespath) +``` + +## Using Jmespath + +There is an interactive function, `jmespath-query-and-show` that you can use to query the currently opened buffer or a file. +To use it with current buffer, you can simply call this function and enter your query. +The output will be shown on a new buffer with name **JMESPath Result**. + +To use this with a different file, you can set the `Universal Argument` using `C-u` or `SPC-u` (using evil). +Then you can enter the file to execute the query on. + +## Read more + +To learn more about JMESPath, visit [Jamespath page](https://jmespath.org/).