|
1 |
| -import { IForeignCodeExtractorsRegistry } from './types'; |
2 |
| -import { RegExpForeignCodeExtractor } from './regexp'; |
3 |
| -import { |
4 |
| - extract_r_args, |
5 |
| - rpy2_args_pattern, |
6 |
| - RPY2_MAX_ARGS |
7 |
| -} from '../magics/rpy2'; |
8 |
| -import { JupyterFrontEndPlugin } from "@jupyterlab/application"; |
9 |
| -import { ILSPCodeExtractorsManager, PLUGIN_ID } from "../tokens"; |
10 |
| - |
11 |
| -function rpy2_code_extractor(match: string, ...args: string[]) { |
12 |
| - let r = extract_r_args(args, -3); |
13 |
| - let code: string; |
14 |
| - if (r.rest == null) { |
15 |
| - code = ''; |
16 |
| - } else { |
17 |
| - code = r.rest.startsWith(' ') ? r.rest.slice(1) : r.rest; |
18 |
| - } |
19 |
| - return code; |
20 |
| -} |
21 |
| - |
22 |
| -function rpy2_args(match: string, ...args: string[]) { |
23 |
| - let r = extract_r_args(args, -3); |
24 |
| - // define dummy input variables using empty data frames |
25 |
| - let inputs = r.inputs.map(i => i + ' <- data.frame();').join(' '); |
26 |
| - let code = rpy2_code_extractor(match, ...args); |
27 |
| - if (inputs !== '' && code) { |
28 |
| - inputs += ' '; |
29 |
| - } |
30 |
| - return inputs; |
31 |
| -} |
32 |
| - |
33 |
| -// TODO: make the regex code extractors configurable |
34 |
| -export let foreign_code_extractors: IForeignCodeExtractorsRegistry = { |
35 |
| - // general note: to match new lines use [^] instead of dot, unless the target is ES2018, then use /s |
36 |
| - python: [ |
37 |
| - // |
38 |
| - // R magics (non-standalone: the R code will always be in the same, single R-namespace) |
39 |
| - // |
40 |
| - new RegExpForeignCodeExtractor({ |
41 |
| - language: 'r', |
42 |
| - pattern: '^%%R' + rpy2_args_pattern(RPY2_MAX_ARGS) + '\n([^]*)', |
43 |
| - extract_to_foreign: rpy2_code_extractor, |
44 |
| - extract_arguments: rpy2_args, |
45 |
| - is_standalone: false, |
46 |
| - file_extension: 'R' |
47 |
| - }), |
48 |
| - new RegExpForeignCodeExtractor({ |
49 |
| - language: 'r', |
50 |
| - pattern: '(?:^|\n)%R' + rpy2_args_pattern(RPY2_MAX_ARGS) + '( .*)?\n?', |
51 |
| - extract_to_foreign: rpy2_code_extractor, |
52 |
| - extract_arguments: rpy2_args, |
53 |
| - is_standalone: false, |
54 |
| - file_extension: 'R' |
55 |
| - }), |
56 |
| - // |
57 |
| - // Standalone IPython magics |
58 |
| - // (script magics are standalone, i.e. consecutive code cells with the same magic create two different namespaces) |
59 |
| - // |
60 |
| - new RegExpForeignCodeExtractor({ |
61 |
| - language: 'python', |
62 |
| - pattern: '^%%(python|python2|python3|pypy)( .*?)?\n([^]*)', |
63 |
| - extract_to_foreign: '$3', |
64 |
| - is_standalone: true, |
65 |
| - file_extension: 'py' |
66 |
| - }), |
67 |
| - new RegExpForeignCodeExtractor({ |
68 |
| - language: 'perl', |
69 |
| - pattern: '^%%(perl)( .*?)?\n([^]*)', |
70 |
| - extract_to_foreign: '$3', |
71 |
| - is_standalone: true, |
72 |
| - file_extension: 'pl' |
73 |
| - }), |
74 |
| - new RegExpForeignCodeExtractor({ |
75 |
| - language: 'ruby', |
76 |
| - pattern: '^%%(ruby)( .*?)?\n([^]*)', |
77 |
| - extract_to_foreign: '$3', |
78 |
| - is_standalone: true, |
79 |
| - file_extension: 'rb' |
80 |
| - }), |
81 |
| - new RegExpForeignCodeExtractor({ |
82 |
| - language: 'sh', |
83 |
| - pattern: '^%%(sh|bash)( .*?)?\n([^]*)', |
84 |
| - extract_to_foreign: '$3', |
85 |
| - is_standalone: true, |
86 |
| - file_extension: 'sh' |
87 |
| - }), |
88 |
| - new RegExpForeignCodeExtractor({ |
89 |
| - language: 'html', |
90 |
| - pattern: '^%%(html --isolated)( .*?)?\n([^]*)', |
91 |
| - extract_to_foreign: '$3', |
92 |
| - is_standalone: true, |
93 |
| - file_extension: 'html' |
94 |
| - }), |
95 |
| - // |
96 |
| - // IPython magics producing continuous documents (non-standalone): |
97 |
| - // |
98 |
| - new RegExpForeignCodeExtractor({ |
99 |
| - language: 'javascript', |
100 |
| - pattern: '^%%(js|javascript)( .*?)?\n([^]*)', |
101 |
| - extract_to_foreign: '$3', |
102 |
| - is_standalone: false, |
103 |
| - file_extension: 'js' |
104 |
| - }), |
105 |
| - new RegExpForeignCodeExtractor({ |
106 |
| - language: 'html', |
107 |
| - pattern: '^%%(html)( .*?)?\n([^]*)', |
108 |
| - extract_to_foreign: '$3', |
109 |
| - is_standalone: false, |
110 |
| - file_extension: 'html' |
111 |
| - }), |
112 |
| - new RegExpForeignCodeExtractor({ |
113 |
| - language: 'latex', |
114 |
| - pattern: '^%%(latex)( .*?)?\n([^]*)', |
115 |
| - extract_to_foreign: '$3', |
116 |
| - is_standalone: false, |
117 |
| - file_extension: 'tex' |
118 |
| - }), |
119 |
| - new RegExpForeignCodeExtractor({ |
120 |
| - language: 'markdown', |
121 |
| - pattern: '^%%(markdown)( .*?)?\n([^]*)', |
122 |
| - extract_to_foreign: '$3', |
123 |
| - is_standalone: false, |
124 |
| - file_extension: 'md' |
125 |
| - }) |
126 |
| - ] |
127 |
| -}; |
128 |
| - |
129 |
| -export const DEFAULT_CODE_EXTRACTORS: JupyterFrontEndPlugin<void> = { |
130 |
| - id: PLUGIN_ID + ':default-extractors', |
131 |
| - requires: [ILSPCodeExtractorsManager], |
132 |
| - activate: (app, extractors_manager: ILSPCodeExtractorsManager) => { |
133 |
| - for (let language of Object.keys(foreign_code_extractors)) { |
134 |
| - for (let extractor of foreign_code_extractors[language]) { |
135 |
| - extractors_manager.register(extractor, language); |
136 |
| - } |
137 |
| - } |
138 |
| - }, |
139 |
| - autoStart: true |
140 |
| -}; |
| 1 | +import { JupyterFrontEndPlugin } from '@jupyterlab/application'; |
| 2 | +import { IPYTHON_CODE_EXTRACTORS } from './ipython'; |
| 3 | +import { IPYTHON_RPY2_CODE_EXTRACTORS } from './ipython-rpy2'; |
| 4 | +import { IPYTHON_SQL_CODE_EXTRACTORS } from './ipython-sql'; |
| 5 | + |
| 6 | +export const DEFAULT_CODE_EXTRACTORS: JupyterFrontEndPlugin<void>[] = [ |
| 7 | + IPYTHON_CODE_EXTRACTORS, |
| 8 | + IPYTHON_RPY2_CODE_EXTRACTORS, |
| 9 | + IPYTHON_SQL_CODE_EXTRACTORS |
| 10 | +]; |
0 commit comments