Skip to content

Commit

Permalink
simplify nb2md script and verify new post
Browse files Browse the repository at this point in the history
  • Loading branch information
benslack19 committed May 29, 2024
1 parent c3a009e commit 8e6415c
Show file tree
Hide file tree
Showing 9 changed files with 1,339 additions and 50 deletions.
1,271 changes: 1,271 additions & 0 deletions _posts/2024-05-28-time-series-varying-intercepts.md

Large diffs are not rendered by default.

64 changes: 14 additions & 50 deletions _posts_drafts/nb2md_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,51 @@


# Create variables, using the notebook name from the argument of the call---------

NB=$1
NB_base=$(echo ${NB} | sed s'|.ipynb||')
NB_md="$NB_base.md"

echo ' '
echo 'File base name: ' ${NB_base}
echo 'Notebook: ' ${NB}
echo 'File base name: ' ${NB_base}
echo 'Markdown file: ' ${NB_md}
echo ' '

# Convert to markdown format ------------------------------------------------------

jupyter nbconvert --to markdown ${NB}


# Move the associated figures files, in the NB_files folder, to _assets--------------------

mv ${NB_base}_files/ ../assets/${NB_base}_files/

echo 'Moving associated figures ' ${NB_base}_files/ ../_assets/${NB_base}_files/
echo ' '

# Edit path of .png files ----------------------------------

# I can't move the file first and edit in place with sed
# Edit path of .png and .svg files ----------------------------------
# I can't edit the file paths in place with sed or over-write the markdown.
# Better to edit first, create a new file in _posts folder, then delete in current folder

# Find all of the references to .png files in the newly moved markdown script and edit with new path---

# Find all of the references to .png and svg files in the newly moved markdown script and edit with new path
# OLD ![png](test_files/test_5_0.png)
# NEW ![png](/assets/test_files/test_5_0.png)

echo 'Before renaming png files'
cat ../_posts/${NB_md} | grep "\[png\](${NB_base}_files"
echo ' '

# sed command was a bit hard to write
# need to use single and double quotes (using comma as separator in sed command
sed 's,'\\[png\]\("$NB_base"_files','\\[png\]\(/assets/"$NB_base"_files',g' ${NB_md} > ../_posts/${NB_md}

echo 'After renaming png files'
cat ../_posts/${NB_md} | grep "\[png\](${NB_base}_files"
echo ' '

# Edit path of .svg files ---------------------------------------
echo 'Before renaming svg files'
cat ../_posts/${NB_md} | grep "\[svg\](${NB_base}_files"
echo 'Before renaming png files, markdown in _post_drafts'
cat ${NB_md} | egrep "svg|png"
echo ' '

# sed command was a bit hard to write
# need to use single and double quotes (using comma as separator in sed command
sed 's,'\\[svg\]\("$NB_base"_files','\\[svg\]\(/assets/"$NB_base"_files',g' ${NB_md} > ../_posts/${NB_md}
# sed -E: Enables extended regular expressions
sed -E 's/!\[(png|svg)\]\(([^)]+)\)/![\1]\(\/assets\/\2)/g' ${NB_md} > ../_posts/${NB_md}

echo 'After renaming svg files'
cat ../_posts/${NB_md} | grep "\[svg\](${NB_base}_files"
echo 'After renaming png files, markdown in _post'
cat ../_posts/${NB_md} | egrep "svg|png"
echo ' '


# Move the markdown file to _posts ----------------------------------
echo 'Moved markdown file ' ${NB_md} ../_posts/${NB_md}

# Remove the lines that say `<IPython.core.display.Javascript object>` (not working)
# Remove the lines that say `<IPython.core.display.Javascript object>`
echo 'Remove random Javascript object text'
# sed '/<IPython.core.display.Javascript object>/d' ./${NB_md} > ./${NB_md}
sed -i '' '/\<IPython.core.display.Javascript object\>/d' ../_posts/${NB_md}
# sed -i '' '/\<IPython.core.display.Javascript object\>/d' ${NB_md}
echo ' '


# Cut out the first line
echo 'Cut out the first line'
# Need to assign to a diff file first. Redirection (>) happens before tail is invoked by the shell.
# https://stackoverflow.com/questions/339483/how-can-i-remove-the-first-line-of-a-text-file-using-bash-sed-script
tail -n +2 ../_posts/${NB_md} > ../_posts/${NB_md}.tmp && mv ../_posts/${NB_md}.tmp ../_posts/${NB_md}
echo ' '

rm ${NB_md}
echo 'Removed markdown file in _posts_drafts folder '
echo ' '

echo "Markdown produced and updated."
echo "cd to parent folder and make sure first line of markdown file is not blank (starts with --- indicating start of YAML)"
echo "Run bundle exec jekyll serve to test page build locally."
echo "Updated markdown produced in _posts folder."
echo "cd to parent folder and run bundle exec jekyll serve to test page build locally."
echo " "
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8e6415c

Please sign in to comment.