ai: add latex rendering

This commit is contained in:
end-4 2024-02-26 22:43:27 +07:00
parent 6676c37e2c
commit 4afa355b0b
5 changed files with 100 additions and 2 deletions

View file

@ -83,4 +83,7 @@ console.log('uwu');
\`\`\`
- Random instruction thing
- To update arch lincox, run \`sudo pacman -Syu\`
\`\`\`tex
\\frac{d}{dx} \\left( \\frac{x-438}{x^2+23x-7} \\right) = \\frac{-x^2 + 869}{(x^2+23x-7)^2}
\`\`\`
`;

View file

@ -3,12 +3,12 @@ import GtkSource from "gi://GtkSource?version=3.0";
import App from 'resource:///com/github/Aylur/ags/app.js';
import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
const { Box, Button, Label, Scrollable } = Widget;
const { Box, Button, Label, Icon, Scrollable } = Widget;
const { execAsync, exec } = Utils;
import { MaterialIcon } from '../../.commonwidgets/materialicon.js';
import md2pango from '../../.miscutils/md2pango.js';
const LATEX_DIR = `${GLib.get_user_cache_dir()}/ags/media/latex`;
const CUSTOM_SOURCEVIEW_SCHEME_PATH = `${App.configDir}/assets/themes/sourceviewtheme.xml`;
const CUSTOM_SCHEME_ID = 'custom';
const USERNAME = GLib.get_user_name();
@ -82,7 +82,69 @@ const TextBlock = (content = '') => Label({
label: content,
});
Utils.execAsync(['bash', '-c', `rm ${LATEX_DIR}/*`])
.then(() => Utils.execAsync(['bash', '-c', `mkdir -p ${LATEX_DIR}`]))
.catch(print);
const Latex = (content = '') => {
const latexViewArea = Box({
// vscroll: 'never',
// hscroll: 'automatic',
attribute: {
render: async (self, text) => {
if (text.length == 0) return;
const styleContext = self.get_style_context();
const fontSize = styleContext.get_property('font-size', Gtk.StateFlags.NORMAL);
const timeSinceEpoch = Date.now();
const fileName = `${timeSinceEpoch}.tex`;
const outFileName = `${timeSinceEpoch}-symbolic.svg`;
const scriptFileName = `${timeSinceEpoch}-render.sh`;
const filePath = `${LATEX_DIR}/${fileName}`;
const outFilePath = `${LATEX_DIR}/${outFileName}`;
const scriptFilePath = `${LATEX_DIR}/${scriptFileName}`;
Utils.writeFile(text, filePath).catch(print);
// Since MicroTex doesn't support file path input properly, we gotta cat it
// And escaping such a command is a fucking pain so I decided to just generate a script
// Note: MicroTex doesn't support `&=`
// You can add this line in the middle for debugging: echo "$text" > ${filePath}.tmp
const renderScript = `#!/usr/bin/env bash
text=$(cat ${filePath} | sed 's/$/ \\\\\\\\/g' | sed 's/&=/=/g')
LaTeX -headless -input="$text" -output=${outFilePath} -textsize=${fontSize * 1.1} -padding=0 -maxwidth=${latexViewArea.get_allocated_width() * 0.85}
`;
Utils.writeFile(renderScript, scriptFilePath).catch(print);
Utils.exec(`chmod a+x ${scriptFilePath}`)
Utils.timeout(100, () => {
Utils.exec(`bash ${scriptFilePath}`);
Gtk.IconTheme.get_default().append_search_path(LATEX_DIR);
self.child?.destroy();
self.child = Gtk.Image.new_from_file(outFilePath);
})
}
},
setup: (self) => self.attribute.render(self, content).catch(print),
});
const wholeThing = Box({
className: 'sidebar-chat-latex',
homogeneous: true,
attribute: {
'updateText': (text) => {
latexViewArea.attribute.render(latexViewArea, text).catch(print);
}
},
children: [Scrollable({
vscroll: 'never',
hscroll: 'automatic',
child: latexViewArea
})]
})
return wholeThing;
}
const CodeBlock = (content = '', lang = 'txt') => {
if (lang == 'tex' || lang == 'latex') {
return Latex(content);
}
const topBar = Box({
className: 'sidebar-chat-codeblock-topbar',
children: [

View file

@ -655,6 +655,15 @@ $colorpicker_rounding: 0.341rem;
@include readingfont;
}
.sidebar-chat-latex {
margin: 0rem 0.682rem;
@if $darkmode ==true {
background-color: white;
}
color: $onBackground;
// background-color: $termbg;
}
.sidebar-chat-codeblock {
@include normal-rounding;
// @include elevation2;

View file

@ -127,6 +127,15 @@ if $(test -d /usr/local/share/icons/Bibata-Modern-Classic); then
else ask_bibata=true
fi
if $ask_bibata;then showfun install-bibata;v install-bibata;fi
if $(test -d /usr/local/bin/LaTex); then
echo -e "\e[33m[$0]: Program \"MicroTex\" already exists, no need to install.\e[0m"
echo -e "\e[34mYou can reinstall it in order to update to the latest version anyway.\e[0m"
ask_MicroTex=$ask
else ask_MicroTex=true
fi
if $ask_MicroTex;then showfun install-MicroTex;v install-MicroTex;fi
#####################################################################################
printf "\e[36m[$0]: 3. Copying\e[97m\n"

View file

@ -86,3 +86,18 @@ install-bibata (){
x sudo cp -r $name /usr/local/share/icons
x cd $base
}
install-MicroTex (){
x mkdir -p $base/cache/MicroTex
x cd $base/cache/MicroTex
try git init -b master
try git remote add origin https://github.com/NanoMichael/MicroTeX.git
x git pull origin master && git submodule update --init --recursive
x mkdir -p build
x cd build
x cmake ..
x make -j32
x sudo mkdir -p /usr/local/bin
x sudo cp ./LaTeX /usr/local/bin/
x cd $base
}