Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Update ImageJ.net Search Function #235

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions _search/server/index-sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging, os, sys
import jekyll, ijsite, tsutil
import tutorials


logger = logging.getLogger('indexer')
Expand All @@ -12,6 +13,8 @@ def load_site(siteroot):
return jekyll.load_jekyll_site(siteroot)
if ijsite.is_imagej_website(siteroot):
return ijsite.load_site(siteroot)
if tutorials.is_imagej_tutorials(siteroot):
return tutorials.load_imagej_tutorials(siteroot)
return None


Expand Down
96 changes: 96 additions & 0 deletions _search/server/tutorials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/env python

# Parse ImageJ tutorials into documents for
# use with their own searchable collection.

import logging, traceback
import json
from parseutil import first_sentence
from pathlib import Path


logger = logging.getLogger(__name__)


def is_imagej_tutorials(root):
java = Path(root) / 'java'
notebooks = Path(root) / 'notebooks'
return java.isdir() and notebooks.isdir()


def parse_java_source(path):
logger.debug(f'Parsing Java source file {path}...')

with open(path) as f:
lines = json.load(f)

# This is dumb -- do we want to do better?
doc = {}
doc['content'] = ''.join(lines)

return doc


def parse_notebook(path):
logger.debug(f'Parsing notebook {path}...')

with open(path) as f:
data = json.load(f)

doc = {}
doc['content'] = ''
for cell in data['cells']:
# TODO: implement process_cell: extract source and output(s) if present
doc['content'] += process_cell(cell)

return doc

# type of cell is dict for reference
# 2 cases: java file or a notebook
# case 1: notebook -> need info inside cells and then info from output lines
# case 2: java file -> need class name and class javadoc for description
def process_cell(cell):
# case 1: notebook

# case 2: java files

return type(cell)


def load_imagej_tutorials(root):
"""
Loads the content from the given imagej/tutorials folder.
See: https://github.com/imagej/tutorials
"""
java = Path(root) / 'java'
notebooks = Path(root) / 'notebooks'
if not java.isdir() or not notebooks.isdir():
raise ValueError(f'The path {root} does not appear to be a Jekyll site.')

logger.info('Loading content...')
documents = []

for javafile in java.rglob("**/*.java"):
try:
doc = parse_java_source(javafile)
if doc:
documents.append(doc)
except:
logger.error(f'Failed to parse {Path}:')
traceback.print_exc()
logger.info(f'Loaded {len(documents)} documents from Java source files')

for nbfile in notebooks.rglob("**/*.ipynb"):
try:
doc = parse_notebook(nbfile)
if doc:
documents.append(doc)
except:
logger.error(f'Failed to parse {Path}:')
traceback.print_exc()
logger.info(f'Loaded {len(documents)} documents from Jupyter notebooks')

return documents

def main():
print("Hello")