Skip to content

Commit

Permalink
Add include option
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbruno committed Nov 16, 2023
1 parent 59e2f16 commit 6ebfdf0
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,67 @@ In comptibility mode, the URLs will not contain commands, just the path for the
To get some examples on how to use compatibility mode, see the folder _compatibility_mode_ inside the demos folder of your BigBashView package.
Claro, aqui está uma documentação em inglês, formatada em Markdown, para a sua implementação de inclusão de conteúdos e execução de scripts no servidor Python com a classe `content_handler`. A documentação inclui uma descrição geral, instruções de uso e exemplos.
---
### HTML Inclusion
To include an HTML file, use the following syntax in your HTML:
```html
<?include html path/to/file.html?>
```
### Script Execution
To execute scripts, embed them within the following tags:
- **Bash:**
```html
<?include bash
# Your bash commands here
?>
```
- **PHP:**
```html
<?include php
# Your PHP code here
?>
```
- **Node.js:**
```html
<?include node
# Your Node.js code here
?>
```
## Examples
### Including an HTML File
```html
<!-- Include a navigation bar -->
<div>
<?include html components/navigation.html?>
</div>
```
### Executing a Bash Script
```html
<!-- List files in the root and home directories -->
<div>
<?include bash
ls /
ls /home
?>
</div>
```
## Legacy Refrences
- Visit [BigLinux Blog](https://biglinux.blogspot.com/2009/07/bigbashview-em-busca-da-revolucao.html) and [BigBashView's section](https://github.com/biglinux/bigbashview/blob/master/bigbashview/usr/share/bigbashview/README.md)
Expand Down
60 changes: 59 additions & 1 deletion bigbashview/usr/lib/bbv/server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .html import HTML
from bbv import globaldata
import subprocess
import re
import os
import web
from shutil import which
Expand Down Expand Up @@ -74,11 +75,68 @@ class content_handler(url_handler):
def called(self, options, content, query):
try:
with open(content) as arq:
return arq.read()
html_content = arq.read()
html_content = self.process_includes(html_content)
return html_content
except UnicodeDecodeError:
with open(content, 'rb') as arq:
return arq.read()
except FileNotFoundError:
web.ctx.status = '404 Not Found'
return "File not found"

def process_includes(self, html_content):
pattern = r'<\?include (\w+) "(.*?)"\?>'
matches = re.finditer(pattern, html_content, re.DOTALL)

for match in matches:
include_type, include_content = match.groups()
include_content = include_content.strip()

if include_type == 'html':
html_content = self.include_html(html_content, include_content, match.group(0))
elif include_type == 'bash':
html_content = self.include_bash(html_content, include_content, match.group(0))
elif include_type == 'php':
html_content = self.include_php(html_content, include_content, match.group(0))
elif include_type == 'node':
html_content = self.include_node(html_content, include_content, match.group(0))

return html_content

def include_html(self, html_content, file_path, original_string):
full_path = os.path.join(os.getcwd(), file_path)
try:
with open(full_path, 'r') as file:
included_html = file.read()
html_content = html_content.replace(original_string, included_html)
except FileNotFoundError:
html_content = html_content.replace(original_string, f"File {full_path} not found")
return html_content

def include_bash(self, html_content, script, original_string):
try:
result = subprocess.check_output(['bash', '-c', script], stderr=subprocess.STDOUT)
html_content = html_content.replace(original_string, result.decode())
except subprocess.CalledProcessError as e:
html_content = html_content.replace(original_string, f"Error executing bash script: {e.output.decode()}")
return html_content

def include_php(self, html_content, script, original_string):
try:
result = subprocess.check_output(['php', '-r', script], stderr=subprocess.STDOUT)
html_content = html_content.replace(original_string, result.decode())
except subprocess.CalledProcessError as e:
html_content = html_content.replace(original_string, f"Error executing PHP script: {e.output.decode()}")
return html_content

def include_node(self, html_content, script, original_string):
try:
result = subprocess.check_output(['node', '-e', script], stderr=subprocess.STDOUT)
html_content = html_content.replace(original_string, result.decode())
except subprocess.CalledProcessError as e:
html_content = html_content.replace(original_string, f"Error executing Node.js script: {e.output.decode()}")
return html_content

class execute_handler(url_handler):
__url__ = '/execute(.*)'
Expand Down

0 comments on commit 6ebfdf0

Please sign in to comment.