Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
didix21 committed Apr 19, 2020
1 parent 7031125 commit 4ff83dd
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 8 deletions.
23 changes: 20 additions & 3 deletions doc/source/examples/Example_Markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Contents
* [Create a Table](#create-a-table)
* [Create Links](#create-links)
* [Add images](#add-images)

* [Html images](#html-images)

# Overview


Expand Down Expand Up @@ -103,7 +104,7 @@ Using ``new_paragraph`` method you can very easily add a new paragraph on your m
mdFile.new_paragraph("This is an example of text in which has been added color, bold and italics text.", bold_italics_code='bi', color='purple')
```

***<font color="purple"> This is an example of text in which has been added color, bold and italics text. </font>***
***<font color="purple">This is an example of text in which has been added color, bold and italics text.</font>***
### New Line Method


Expand Down Expand Up @@ -143,7 +144,7 @@ mdFile.write(' \n')
mdFile.write('Align Text to center', align='center')
```
***``bold_italics_code``***
<font color="green"> Text color </font>
<font color="green">Text color</font>
<center>Align Text to center</center>

## Create a Table
Expand Down Expand Up @@ -237,6 +238,22 @@ You can add inline images using ``new_reference_image`` method. Method will retu
mdFile.new_line(mdFile.new_reference_image(text='snow trees', path='../images/photo-of-snow-covered-trees.jpg', reference_tag='im'))
```
![snow trees][im]
## Html images

### Change size to images


With ``Html.image`` you can change size of images in a markdown file. For example you can dothe following for changing width: ``mdFile.new_paragraph(Html.image(path=path, size='200'))``

<img src="../images/sunset.jpg" width="200"/>

Or maybe only want to change height: ``mdFile.new_paragraph(Html.image(path=path, size='x500'))``

<img src="../images/sunset.jpg" height="500"/>

Or change width and height: ``mdFile.new_paragraph(Html.image(path=path, size='900x500'))``

<img src="../images/sunset.jpg" width="900" height="500"/>


[1]: https://github.com/didix21/mdutils
Expand Down
21 changes: 21 additions & 0 deletions doc/source/examples/Example_Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


from mdutils.mdutils import MdUtils
from mdutils import Html

mdFile = MdUtils(file_name='Example_Markdown', title='Markdown File Example')

Expand Down Expand Up @@ -250,6 +251,26 @@
mdFile.insert_code("mdFile.new_line(mdFile.new_reference_image(text='{}', path='{}', reference_tag='im'))".format(image_text, path))
mdFile.new_line(mdFile.new_reference_image(text=image_text, path=path, reference_tag='im'))

# ************************************************* Html Image *******************************************************

mdFile.new_header(2, "Html images")

# *********************************************** Size Image *******************************************************

mdFile.new_header(3, "Change size to images")
path="../images/sunset.jpg"

mdFile.new_paragraph("With ``Html.image`` you can change size of images in a markdown file. For example you can do"
"the following for changing width: ``mdFile.new_paragraph(Html.image(path=path, size='200'))``")

mdFile.new_paragraph(Html.image(path=path, size='200'))

mdFile.new_paragraph("Or maybe only want to change height: ``mdFile.new_paragraph(Html.image(path=path, size='x500'))``")
mdFile.new_paragraph(Html.image(path=path, size='x500'))

mdFile.new_paragraph("Or change width and height: ``mdFile.new_paragraph(Html.image(path=path, size='900x500'))``")
mdFile.new_paragraph(Html.image(path=path, size='900x500'))

# Create a table of contents
mdFile.new_table_of_contents(table_title='Contents', depth=2)
mdFile.create_md_file()
Expand Down
Binary file added doc/source/images/sunset.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions mdutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from mdutils.fileutils import fileutils
from mdutils.mdutils import MdUtils
from mdutils.tools import Header, Link, Image, TextUtils, Table, TableOfContents
from mdutils.tools import Html
65 changes: 63 additions & 2 deletions mdutils/tools/Html.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
class Html:

@staticmethod
def paragraph(text, align=None):
def paragraph(text: str, align: str = None):
"""
:param text:
:type text: str
:param align: ``center`` or ``right``.
:type align: str
:return: ``"<p align=\"{}\">{}\\n</p>".format(align, text)``.
Expand All @@ -28,3 +27,65 @@ def paragraph(text, align=None):
raise KeyError

return '<p align="{}">\n {}\n</p>'.format(align, text)

@classmethod
def image(cls, path: str, size: str = None):
"""
:param path:
:type path: str
:param size:
:type size: str
:return:
"""

return cls.__html_image(path=path, size=size)

@classmethod
def __html_image(cls, path: str, size: str = None):
if size:
return '<img src="{}" {}/>'.format(path, HtmlSize.size_to_width_and_height(size=size))
return '<img src="{}" />'.format(path)


class HtmlSize:
@classmethod
def size_to_width_and_height(cls, size: str) -> str:
size = cls.__pre_process_size(size=size)
if size.isdigit():
return cls.__get_width(size=size)

if size.startswith('x'):
height = size[1:]
if height.isdigit():
return cls.__get_height(size=height)

raise SizeBadFormat(size)

width_height = size.split('x')

if len(width_height) == 2:
if width_height[0].isdigit() and width_height[1].isdigit():
return "{} {}".format(cls.__get_width(width_height[0]), cls.__get_height(width_height[1]))

raise SizeBadFormat(size)

@classmethod
def __pre_process_size(cls, size: str) -> str:
no_spaces = size.replace(" ", "")
return no_spaces.lower()

@classmethod
def __get_width(cls, size: str) -> str:
return 'width="{}"'.format(int(size))

@classmethod
def __get_height(cls, size: str):
return 'height="{}"'.format(int(size))


class SizeBadFormat(Exception):
"""Raise exception when size does not match the expected format"""
def __init__(self, message):
Exception.__init__(self, "Unexpected format: {}. Expected: '<int>', 'x<int>' or '<int>x<int>'".format(message))
pass
4 changes: 2 additions & 2 deletions mdutils/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .Link import Inline
from .TextUtils import TextUtils
from .Html import Html
from mdutils.tools import Header, Table, TableOfContents, Link
from .Html import Html, SizeBadFormat
from mdutils.tools import Header, Table, TableOfContents, Link, Html

87 changes: 86 additions & 1 deletion tests/test_tools/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
# MIT License: (C) 2018 Dídac Coll

from unittest import TestCase
from mdutils.tools.Html import Html
from mdutils.tools.Html import Html, SizeBadFormat, HtmlSize


class TestHtml(TestCase):

def setUp(self):
self.text = "my text"
self.path = "./my_path"

def test_paragraph(self):
expected_paragraph = "<p align=\"{}\">\n {}\n</p>".format("left", self.text)
Expand All @@ -35,3 +36,87 @@ def test_paragraph_when_invalid_align_is_passed(self):

self.fail()

def test_image_path(self):
expected_image = '<img src="{}" />'.format(self.path)
actual_image = Html.image(path=self.path)

self.assertEqual(expected_image, actual_image)

def test_image_size_width(self):
size = '200'
expected_image = '<img src="{}" width="{}"/>'.format(self.path, size)
actual_image = Html.image(path=self.path, size=size)

self.assertEqual(expected_image, actual_image)

def test_image_size_height(self):
expected_image = '<img src="{}" height="200"/>'.format(self.path)
actual_image = Html.image(path=self.path, size='x200')

self.assertEqual(expected_image, actual_image)

def test_image_size_width_height(self):
size = '200'
expected_image = '<img src="{}" width="{}" height="{}"/>'.format(self.path, size, size)
actual_image = Html.image(path=self.path, size='200x200')

self.assertEqual(expected_image, actual_image)


class TestHtmlSize(TestCase):

def test_raise_exception(self):
try:
HtmlSize.size_to_width_and_height(size='dd')
except SizeBadFormat:
return

self.fail()

def test_size_to_width_height_when_providing_number(self):
expected = 'width="200"'
actual = HtmlSize.size_to_width_and_height(size='200')

self.assertEqual(expected, actual)

def test_size_to_width_height_when_providing_x_int(self):
expected = 'height="200"'
actual = HtmlSize.size_to_width_and_height(size='x200')

self.assertEqual(expected, actual)

def test_size_to_width_height_when_providing_x_str_int(self):
try:
HtmlSize.size_to_width_and_height(size='xD200')
except SizeBadFormat:
return

self.fail()

def test_size_to_width_height_when_providing_int_x_int(self):
expected = 'width="200" height="300"'
actual = HtmlSize.size_to_width_and_height(size='200x300')

self.assertEqual(expected, actual)

def test_size_to_width_height_when_providing_int_whitespace_X_int(self):
expected = 'width="200" height="300"'
actual = HtmlSize.size_to_width_and_height(size='200 X300')

self.assertEqual(expected, actual)

def test_size_to_width_height_when_providing_x_str_int(self):
try:
HtmlSize.size_to_width_and_height(size='200dx200')
except SizeBadFormat:
return

self.fail()

def test_size_to_width_height_when_providing_x_str_int(self):
try:
HtmlSize.size_to_width_and_height(size='fx200')
except SizeBadFormat:
return

self.fail()

0 comments on commit 4ff83dd

Please sign in to comment.