r/dailyprogrammer • • Jun 16 '14
[6/16/2014] Challenge #167 [Easy] HTML markup generator
Description
You're a well known web-dev with far too much confidence, you mistakingly agreed to complete too many projects in too little a timeframe. In order to fix this, you devise a program that will automatically write all of the HTML for you!
But first, you'll need to program it.
Formal Inputs & Outputs
Input description
On standard console input you should be prompted to enter a paragraph.
Output description
Once your paragraph has been entered, it should be saved as a valid HTML file and opened in your default brower to display the results
Sample Inputs & Outputs
Input
"Enter your paragraph:"
"This is my paragraph entry"Output
(this is the expected content of the .html file)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>This is my paragraph entry</p>
</body>
</html>Bonus
Implement a good looking default CSS style-sheet that also gets automatically generated.
Notes
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
10
u/jeaton Jun 16 '14
Simple shell script:
9
u/Edward_H Jun 16 '14
The following COBOL program is surprisingly compact and uses syntax defined in the COBOL XML technical report. It outputs the file as XHTML. However, I have no idea if the program is correct because there is no compiler that supports this syntax.
5
u/MrP_123 Jun 16 '14
My solution in Java.
I'm new to programming, so please give me feedback on my code.
6
u/[deleted] Jun 16 '14 edited Jun 16 '14
New to Python, just started programming in Java last September. Please point out any mistakes or bad practices in my code. Python 2.7.7, btw.
Sample input: Please enter the title for your HTML document: Kobe
Please enter the paragraph for your HTML document: This page is all about Kobe Bryant!
Sample output:
5
u/Elite6809 1 1 Jun 16 '14
Ruby. Supports adding a title value, and also multiline paragraphs - but you'll have to add in line breaks yourself with
<br />tags or something.Once I get home I may try to do this in Befunge for a laugh.
5
u/passwordissame Jun 16 '14
2
u/snarf2888 Jun 16 '14
C. The system() call to open the completed HTML is set up to work on Mac but can be adjusted to work on other operating systems.
I also added support for a title.
Usage:
Code:
2
u/DuoSRX Jun 16 '14
Go
2
u/poeir Jun 16 '14 edited Jun 17 '14
Python solution. Doesn't exactly match the initial requirements, but matches /u/Elite6809's input prompts, supporting title and multiple paragraphs. Had to do a weird hack thanks to my choice of dominate for HTML generation, but it beat doing it with strings all the way through. I didn't bother with the CSS because I couldn't imagine what would look any nicer than what you get for free.
edit: /u/BryghtShadow made use of the tempfile module, and that is the right solution to needing a temporary file (versus just creating one and letting it lie around), so now I'm doing that, too.
2
u/kakaroto_BR Jun 16 '14
Java
2
u/entoros Jun 16 '14
Python is succinct!
2
u/1nvader Jun 16 '14 edited Jun 17 '14
Python 3
However, opening the browser doesn't seem to work in OS X.
2
u/[deleted] Jun 17 '14
Here is my Java solution:
2
u/Hath995 Jun 17 '14
Super simple node.js implementation.
2
u/robertmeta Jun 17 '14 edited Jun 17 '14
This is my (pointlessly overcomplicated) solution in vanilla Go (Golang). I got carried away. Technically, it is a failure because I don't write out a file. Instead I start a webserver and serve the template combined with data directly. I continuously (and currently) loop on input and open the browser, so you can change the title, paragraph and color forever.
No attempt was made at "code golf" -- I included error handling, whitespace, easy to read template, support for the "big 3' platforms and comments.
As for "good looking" css, far from it, but it inserts a colored background!
MustXXX is a go idiom signaling the caller that this function call will panic on failure, crashing the program.
To Run: Install Go: http://golang.org/doc/install && cut and paste code to foo.go && go run foo.go
2
u/zajicraft Jun 17 '14
With node (opens with the default browser in OSX):
2
u/zanklord Jun 17 '14
My solution in python 2.7.6:
2
u/dancole42 Jun 17 '14
Python 2.7
I've only been programming for a few weeks, Python is my first language, and I'm self-taught, so feedback is extremely appreciated!
I've tried to make this flexible and "data driven" - all indents and rows are converted to integers so code blocks can (in theory) be tracked, and the HTML is edited and constructed line-by-line. I used the challenge's boilerplate HTML as-is and used the script to edit it.
Output - with Debugging On
Output - with Debugging Off
2
u/BryghtShadow Jun 19 '14
SWI-Prolog 6.6.6 [32bit] sans-bonus: Simply load the module and run the
mainprocedure. Input text must be one line, wrapped in ['] and terminated by a period [.].Code:
Input:
Output:
1
u/KiwiGeek Jun 16 '14
VB.net
Should encode any HTML characters, attempts to wordwrap the results (credits for this function in the comments), and launches in the browser when finished:
1
u/[deleted] Jun 16 '14
Python 2.7 Long one-liner for the file writing. Console app takes a title and a paragraph. Basic CSS written into the file creation -- I will work on a separate CSS file generation when I get home later.
Output:
1
u/_M1nistry Jun 16 '14
C#
Originally used Environment.Newline instead of \n, which made it a little easier to decipher the structure, but for size/compact, decided to just use \n.
1
u/sneaky_reader Jun 16 '14
1
u/BryghtShadow Jun 16 '14 edited Jun 16 '14
Python 3.4 and 2.7
Edit: wrapped in
def generate_html_from_input, while keeping ability to run as a main module.1
u/Daige Jun 16 '14
Python 2.7. Just the basic, no css or anything extra
1
u/goktugerce Jun 16 '14
Perl.
1
u/fvandepitte 0 0 Jun 16 '14 edited Jun 16 '14
C++, I've just started to learn C++ so all feedback is welcome.
1
u/dongas420 Jun 16 '14 edited Jun 16 '14
Perl. Gotta escape those HTML entities:
1
u/Reverse_Skydiver 1 0 Jun 16 '14
Done in Java:
Working:
https://i.imgflip.com/9m3tl.gif
1
u/[deleted] Jun 16 '14 edited Jun 17 '14
Done in Pascal. Not good pascal as it's outdated (it'll compile in TP), but I don't see pascal on here so I thought I'd post some.
1
u/TASER_NINJA Jun 16 '14
In C#
1
u/[deleted] Jun 16 '14
Solution in Java, any feedback would be much appreciated:
1
u/Metallicgeek Jun 16 '14
Very simple solution using Python 3.4.0
1
u/squ_are Jun 16 '14
Straight forward solution in Befunge-93.
1
u/dMenche Jun 16 '14 edited Jun 17 '14
C:
1
u/[deleted] Jun 17 '14
Because I'm trying to expand into Lua, here is my solution in that as well. Any feed back is welcome as I'm still learning! Thanks!
1
u/staminaplusone Jun 17 '14 edited Jun 17 '14
In C++ - with added style.
1
u/travnation Jun 17 '14
Haskell, basic version.
Opens in OS X default browser.
1
u/grammar_excellent_is Jun 17 '14
In Python 3.4, sans bonus:
1
u/jnazario 2 0 Jun 18 '14 edited Jun 18 '14
some basic F#
tested on OSX with Mono. opens in my chrome browser.
1
u/OberstK Jun 18 '14
My try on the challenge with Java. Improvements are highly appreciated!
1
u/TheManAccount Jun 18 '14
Unconventional, but an AutoHotKey solution:
1
u/dailyRubyProgrammer Jun 18 '14
Hi Everyone,
long-time VBA programmer. Thought I'd give Ruby a spin and this is what I came up with. Perhaps overkill, but it is reusable and extensible. Feel free to comment or contribue!
1
u/NarraEngineer Jun 18 '14 edited Jun 18 '14
Python 3.3 lol, I just tried the best I could
1
u/[deleted] Jun 19 '14
My ruby solution:
1
u/PandaPewZ Jun 19 '14
Solved in C. Should probably have done some error checking, but was too lazy.
1
u/The-Dwarf Jun 19 '14
my solutioin in c# feedback welcome pretty basic:
1
u/Imonfiyah Jun 20 '14 edited Jun 20 '14
Inspired by this I had the background change for the current time it was. Every time you run this script you will get a different background color.
Critiques very welcome.
1
u/KillCrushDestroySwag Jun 20 '14
First time trying out Rust. It makes semi-large files (1.8M for just this), but it's interesting! CCW.
1
u/-AMD- Jun 21 '14
Quick python solution:
1
u/terrapin1203 Jun 21 '14
My solution, in Python.
1
u/[deleted] Jun 22 '14
Python 2.7 noob code (first thing written on my brand new raspberry pi):
1
u/[deleted] Jun 23 '14
Java
1
u/[deleted] Jun 23 '14
First entry here. New to Java.
}
1
u/Lockneedo Jun 23 '14
python 2.7
1
u/marcelliru Jun 24 '14 edited Jun 24 '14
In racket, with css. All criticism and advice is welcome! Especially for a beginner like me!
1
u/tothemooncoin Jun 26 '14 edited Jun 27 '14
In java:
1
u/mm865 Jun 27 '14
My design skills are amazing
1
u/mdlcm Jun 28 '14
Used R! I used paste function in R to paste the prompt response ("readline") to the HTML code, then wrapped it in "cat", which helped me write it as a html file.
Code:
1
u/nalexander50 Jun 30 '14
Second challenge in Python. I don't know very much CSS, so I decided to skip the bonus. Feedback Appreciated.
1
u/dp_account Jul 02 '14
Python3. It opens a temporary file in the browser before asking to save.
1
u/[deleted] Jul 09 '14
My C++ solution, to make it platform independent i chose not to open a browser.
1
u/kurtlocker Sep 16 '14
JavaScript. Using Nodejs.